Range.Findtypes不匹配错误

这是我的代码:

如果在表单“Whereused”的B列中find“Itemschedule”表的A列中的值,则需要我的代码在单元格中inputTRUE。 获取“types不匹配”错误。 如果我将“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 

我尝试了很多,但无法弄清楚。 我相信这是愚蠢的。
请帮忙。

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

 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。

字典更有效地find重复的值。

 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