試戴
Sub foo()
Dim vals As Variant
vals = Array("5", "9", "12", "-1")
Dim LastRow As Integer
Dim i As Long
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If IsInArray(ActiveSheet.Range("A" & i).Value, vals) Then
ActiveSheet.Range("B" & i).Value = "Value Exists"
End If
Next i
End Sub
Function IsInArray(key As Variant, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, key)) > -1)
End Function
參考。 HERE
修訂
Sub foo()
Dim vals As Variant
vals = Array("5", "9", "12", "-1")
Dim LastRow As Integer
Dim i As Long
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Not IsEmpty(ActiveSheet.Range("A" & i)) Then
If IsInArray(ActiveSheet.Range("A" & i).Value, vals) Then
ActiveSheet.Range("B" & i).Value = "Value Exists"
End If
End If
Next i
End Sub
Function IsInArray(stringToBeFound As Variant, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
您正在嘗試一個**單值**比較的** **陣列。你需要一個內部循環或一個'Filter()'。 –
您正在比較單元值與孔陣列 - 這會引發錯誤。循環遍歷數組中的所有條目,編寫一個函數來執行檢查(也可以循環)或使用「Dictionary」而不是數組 – FunThomas
您也不能將一個簡單變量聲明爲一個String,然後將一個數組到它。 'vals'應該是一個變體,然後你可以循環它。 – Rory