Sub highlight()
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number As Variant
number = Array(9811, 7849)
Set w = ThisWorkbook
Set sh = w.Worksheets("Sheet1")
sh.Select
Cells.Find("hello").Select
ActiveCell.Offset(1, 0).Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
For x = 1 To k
For j = 0 To UBound(number)
If ActiveCell.Value <> number(j) Then
Selection.Interior.Color = vbYellow
Else
Selection.Interior.ColorIndex = xlNone
Exit For
End If
Next j
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next x
End Sub
上面的代碼工作得很好。
現在,當我從我的GUI發送相同的數字(9811,7849)到下面的vba代碼時,它們被存儲爲phm =「9811,7849」。所以陣列(「9811,7849」)給了我不正確result.Any想法如何使它正常工作\數組中的字符串給出錯誤
sub highlight(phm as variant)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number As Variant
number = Array(phm)
這是不可讀。修正你的縮進,解釋你的代碼,解釋什麼結果是不正確的。 – Amit
縮進是正確的..我已經發布了兩個代碼。第一個是我已經嘗試過的,第二個是我想要的 –
不正確意味着所有的單元格都被突出顯示,而不是錯誤的單元格。我知道錯誤是因爲:number = Array(「9811,7849」)。我無法修復它 –