-2

vb6で整数の配列から整数を見つける方法について、誰でもアイデアを持っていますか?

 Dim myArray(2) As Integer
myArray(1) = 1001
myArray(2) = 1002

Dim searchTerm As Integer
searchTerm = 1005

Dim flag As Boolean
flag = True

Dim temp As Variant

For Each temp In myArray
If temp = searchTerm Then
flag = False
End If
Next temp

If flag = False Then
MsgBox "find"
End If

For Each ステートメントを使用して解決策を得ましたが、Do..Loop を使用して解決策が必要ですか??

編集

Dim myArray(2) As Integer 
myArray(0) = 1000 
myArray(1) = 1001 
myArray(2) = 1002 

'Initialise Search Term 
Dim searchTerm As Integer 
searchTerm = 1001 

'Check if a value exists in the Array 
If UBound(Filter(myArray, searchTerm)) >= 0 And searchTerm <> "" Then 
  MsgBox ("Search Term SUCCESSFULLY located in the Array") 
Else 
  MsgBox ("Search Term could NOT be located in the Array") 
End If
4

2 に答える 2

2

簡単にできます:

Dim i As Integer, found As Boolean
Do While i <= UBound(myArray) And Not found
    If (myArray(i) = searchTerm) Then
        found = True
    Else
        i = i+1
    End If
Loop

If (found) Then Msgbox "found @ " & i
于 2012-06-25T10:56:25.737 に答える
0

以下のコードはファイルを動作させます

Dim myArray(2) As Integer
myArray(0) = 1000
myArray(1) = 1001
myArray(2) = 1002

Dim searchTerm As Integer
searchTerm = 1005

Dim flag As Boolean
flag = True

Dim i As Integer
Dim lb As Integer
Dim hb As Integer
lb = LBound(myArray)
hb = UBound(myArray)

Do While (lb < hb)
  Dim j As Integer
  If searchTerm = myArray(j) Then
    flag = False
  End If
  j = j + 1
  lb = lb + 1
Loop

If flag = False Then
  MsgBox "find"
Else
  MsgBox "not find"
End If
于 2012-06-25T10:57:53.603 に答える