简体   繁体   English

VBA中的类型不匹配错误

[英]Type mismatch error in VBA

This code to is to search each element from column A in worksheet 6 to be existing in Column A in worksheet 3 此代码用于从工作表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

Try this code - you should use the Find method of the Range object to look for a specific value: 尝试以下代码-您应该使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM