Range.Find类型不匹配错误

时间:2016-06-14 02:07:10

标签: excel vba excel-vba

这是我的代码:

如果在单词“Itemschedule”的A列中找到一个值存在于“Whereused”表的B列中,则需要我的代码在单元格中输入TRUE。出现“类型不匹配”错误。如果我将“st = Sheets(...”行更改为.Value而不是.Text,或者如果我将.Find行更改为LookIn:= xlFormulas而不是Lookin:= xlValues,则它会给出相同的错误,而不管两者的结合。

Private Sub CommandButton1_Click()
Dim rowLast As Integer
Dim str As String
Dim cell As Range

Sheets("Itemschedule").ListObjects("Table2").Range.AutoFilter
rowLast = Sheets("Itemschedule").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Itemschedule").ListObjects("Table2").Resize Range("A1:E" & rowLast)
Sheets("Itemschedule").Range("A" & rowLast + 1 & ":E" & Rows.Count).ClearContents
For i = 2 To rowLast
    str = Sheets("Itemschedule").Cells(i, "A").Text
    With Sheets("Whereused").Range("B:B")
        Set cell = .Find(What:=str, After:=.Range("B1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With
    If cell Is Nothing Then
        Sheets("Itemschedule").Cells(i, "E").Value = "FALSE"
    Else
        Sheets("Itemschedule").Cells(i, "E").Value = "TRUE"
    End If
Next
On Error Resume Next
Sheets("Itemschedule").ListObjects("Table2").Range.AutoFilter
Sheets("Itemschedule").Range("A1:E" & rowLast).AutoFilter Field:=1, Criteria1:="FALSE"
Sheets("Itemschedule").Range("A1:E" & rowLast).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Sheets("Itemschedule").ListObjects("Table2").Resize Range("A1:E" & rowLast)
Sheets("Itemschedule").ListObjects("Table2").Range.AutoFilter

End Sub  

我已经尝试了很多,但我无法弄明白。我确定这是愚蠢的事 请帮忙。

2 个答案:

答案 0 :(得分:1)

您的错误是由于After参数不在您正在搜索的范围内。这部分:

With Sheets("Whereused").Range("B:B")
        Set cell = .Find(What:=str, After:=.Range("B1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With

应该是:

With Sheets("Whereused").Range("B:B")
        Set cell = .Find(What:=str, After:=Sheets("Whereused").Range("B1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With

否则.Range("B1")相对于Range("B:B"),因此指的是C1。

答案 1 :(得分:0)

字典在查找重复值方面更有效。

Sub CommandButton1_Click()
    
    Dim keyword As String, keyvalue As Variant
    Dim rowLast As Long, x As Long
    Dim dicItems
    Set dicItems = CreateObject("scripting.dictionary")
    
    With Sheets("Whereused")
        rowLast = .Cells(Rows.Count, "A").End(xlUp).Row
        For x = 2 To rowLast
            keyword = .Cells(x, 2)
            keyvalue = .Cells(x, 2)
            'Add Key Value pairs to Dictionary
            If Not dicItems.Exists(keyword) Then dicItems.Add keyword, keyvalue
        Next
    End With

    With Sheets("Itemschedule")
        rowLast = .Cells(Rows.Count, "A").End(xlUp).Row
        For x = 2 To rowLast
            keyword = .Cells(x, 1)
            .Cells(x, 2) = dicItems.Exists(keyword)
        Next
    End With
End Sub

但是正确的方法是在Itemchedule B列中放置一个WorkSheet公式。

  

= COUNTIF(表1 [[#This Row],[Items]],Table2 [[#Headers],[Items]])> 0