在VBA中键入不匹配错误

时间:2016-05-23 11:48:16

标签: vba

此代码用于搜索工作表6中A列中的每个元素,使其存在于工作表3的A列中

Sub checkpanvalueS()
Dim lastRow1 As Long
Dim lastRow2 As Long
lastRow1 = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
lastRow2 = Sheet6.Cells(Rows.Count, 1).End(xlUp).Row
Dim myArr As Variant
'Dim myArr2 As Variant

'For i = 2 To lastRow1
'myArr(i) = Sheet3.Cells(i, 1)
 myArr = Sheet3.Range("A2:A" & lastRow1)
 'myArr2 = Sheet6.Range("A2:A" & lastRow2)

'Next i
' For i = 2 To lastRow1
For m = 2 To lastRow2
 'if UBound(Filter(myArr, Sheet6.Cells(m, 1))) > -1 and  then
' MsgBox "All Yellow highlighted pan number (Column A ) should  not be one from ptimary Cards ."

    ' If UBound(Filter(myArr, myArr(i))) >= 0 And myArr(i) <> "" Then
      ' If IsInArray(Sheet6.Cells(m, 1), myArr) Then
     If Filter(myArr, Sheet6.Cells(m, 1)) = "" Then
       '  MsgBox ("Search Term SUCCESSFULLY located in the Array")
          Range("A" & m).Interior.Color = vbYellow
          MsgBox (" These pan numbers should'nt be equal to existing primary cards")

     End If
 Next m
 ' Next i
End Sub

1 个答案:

答案 0 :(得分:0)

尝试此代码 - 您应该使用Range对象的Find方法来查找特定值:

Public Sub HighlightItems()

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rngSearch1 As Range
    Dim rngSearch2 As Range
    Dim rngCell As Range
    Dim rngFound As Range

    Set ws1 = ThisWorkbook.Worksheets("Sheet6")
    Set ws2 = ThisWorkbook.Worksheets("Sheet3")
    Set rngSearch1 = ws1.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
    Set rngSearch2 = ws2.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)

    For Each rngCell In rngSearch1
        Set rngFound = rngSearch2.Find(rngCell.Value)
        If Not rngFound Is Nothing Then
            rngCell.Interior.Color = vbYellow
            Debug.Print ws1.Name & "!" & rngCell.Address & " equals " & ws2.Name & "!" & rngFound.Address
        End If
    Next

End Sub