【发布时间】:2020-04-27 08:12:40
【问题描述】:
我正在尝试编写一个小循环来检查所选范围是否包含数组中的任何值。
Sub test()
Dim DirArray As Variant
Dim i As Integer
'define array
DirArray = Sheets("Blad1").Range("A1:A311").Value
'Loop trough array
For i = 1 To UBound(DirArray)
'Activate the sheet with the Range
Sheets("Blad1").Activate
'Go through range of values
If DirArray = Cells(i, 2) Then
MsgBox "it contains the value"
End If
Next i
End Sub
我认为我使用Cells(i,2) 会出错,它说类型不匹配。我已经看了很长时间了,我想我错过了一些明显的东西。
任何帮助或反馈将不胜感激!
【问题讨论】:
-
您不能将数组与任何东西进行比较。使用两个嵌套循环。
-
你必须在循环中指定数组的具体元素,但是使用 MATCH 来避免循环的需要。