错误 1004:无法获取 WorksheetFunction class 的搜索 属性
Error 1004: Unable to get the Search property of the WorksheetFunction class
我反复收到错误 1004:无法为以下代码获取 WorksheetFunction class 的搜索 属性。基本上,我试图确定指定范围内的每个单元格是否包含满足条件的值 - 如果满足此条件,则应将类别分配给同一工作表中另一个单元格中的值:
Option Explicit
Sub Lookup_Category()
Dim Cell As Range, Narration As Range
Dim Counter As Integer
Set Narration = ThisWorkbook.ActiveSheet.Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Narration
Select Case Cell
Case Application.WorksheetFunction.Search("LICIOUS", Cell) > 1
Cells(Cell.Row, 8) = "Licious"
Case Application.WorksheetFunction.Search("BILLDESK.ELECTRICITY", Cell) > 1
Cells(Cell.Row, 8) = "Electricity"
Case Application.WorksheetFunction.Search("PLAYSTATIONNETWO", Cell) > 1
Cells(Cell.Row, 8) = "PlayStation"
Case Application.WorksheetFunction.Search("NEFT CR", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IMPS", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IB FUNDS TRANSFER CR", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IB BILLPAY DR-HDFCYA-463917XXXXXX5057", Cell) > 1
Cells(Cell.Row, 8) = "Credit Card Payment"
Case Application.WorksheetFunction.Search("POS 416021XXXXXX5159 SWIGGY", Cell) > 1
Cells(Cell.Row, 8) = "Dining"
Case Application.WorksheetFunction.Search("POS 416021XXXXXX5159 SWIGGY DASH", Cell) > 1
Cells(Cell.Row, 8) = "Groceries"
Case Application.WorksheetFunction.Search("UPI-VODAFONE IDEA LTD-VILPOSKAR", Cell) > 1
Cells(Cell.Row, 8) = "Mobile Bill"
Case Application.WorksheetFunction.Search("UPI-SS LIQOURS", Cell) > 1
Cells(Cell.Row, 8) = "Alcohol"
Case VBA.Left(Cell.Value, 3) = "REV"
Cells(Cell.Row, 8) = "Refunds"
Case Else
Cells(Cell.Row, 8) = ""
End Select
Next Cell
End Sub
这不需要 VBA,如果没有必要,我建议不要 VBA。假设您有这样的数据设置:
B 列是您要搜索的文本,H 列是您希望输出的位置(以匹配代码的作用)。我在 M 和 N 列中创建了一个 table(尽管它可以在任何地方,甚至在不同的 sheet 上),其中包含您要查找的内容以及应该匹配的内容 return。
在单元格 H2 中复制下来的是这个公式:
=IF(LEFT(B2,3)="REV","Refund",IFERROR(INDEX($N:$N,MATCH(999,SEARCH($M:$M,B2))),""))
如果绝对必须是 VBA,那么我建议对 Like
运算符使用 Select Case True
,如下所示:
Sub Lookup_Category()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.ActiveSheet
Dim rNarration As Range: Set rNarration = ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp))
If rNarration.Row < 2 Then Exit Sub 'No data in range
Dim rCell As Range, rTarget As Range, sValue As String
For Each rCell In rNarration.Cells
Set rTarget = ws.Cells(rCell.Row, 8)
sValue = UCase(rCell.Value)
Select Case True
Case sValue Like "*LICIOUS*": rTarget.Value = "Licious"
Case sValue Like "*BILLDESK.ELECTRICITY*": rTarget.Value = "Electricity"
Case sValue Like "*PLAYSTATIONNETWO*": rTarget.Value = "PlayStation"
Case sValue Like "*NEFT CR*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IMPS*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IB FUNDS TRANSFER CR*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IB BILLPAY DR-HDFCYA-463917XXXXXX5057*": rTarget.Value = "Credit Card Payment"
Case sValue Like "*POS 416021XXXXXX5159 SWIGGY DASH*": rTarget.Value = "Groceries"
Case sValue Like "*POS 416021XXXXXX5159 SWIGGY*": rTarget.Value = "Dining"
Case sValue Like "*UPI-VODAFONE IDEA LTD-VILPOSKAR*": rTarget.Value = "Mobile Bill"
Case sValue Like "*UPI-SS LIQOURS*": rTarget.Value = "Alcohol"
Case Left(sValue, 3) = "REV": rTarget.Value = "Refunds"
Case Else: rTarget.ClearContents
End Select
Next rCell
End Sub
我反复收到错误 1004:无法为以下代码获取 WorksheetFunction class 的搜索 属性。基本上,我试图确定指定范围内的每个单元格是否包含满足条件的值 - 如果满足此条件,则应将类别分配给同一工作表中另一个单元格中的值:
Option Explicit
Sub Lookup_Category()
Dim Cell As Range, Narration As Range
Dim Counter As Integer
Set Narration = ThisWorkbook.ActiveSheet.Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Narration
Select Case Cell
Case Application.WorksheetFunction.Search("LICIOUS", Cell) > 1
Cells(Cell.Row, 8) = "Licious"
Case Application.WorksheetFunction.Search("BILLDESK.ELECTRICITY", Cell) > 1
Cells(Cell.Row, 8) = "Electricity"
Case Application.WorksheetFunction.Search("PLAYSTATIONNETWO", Cell) > 1
Cells(Cell.Row, 8) = "PlayStation"
Case Application.WorksheetFunction.Search("NEFT CR", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IMPS", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IB FUNDS TRANSFER CR", Cell) > 1
Cells(Cell.Row, 8) = "Inbound Transfers"
Case Application.WorksheetFunction.Search("IB BILLPAY DR-HDFCYA-463917XXXXXX5057", Cell) > 1
Cells(Cell.Row, 8) = "Credit Card Payment"
Case Application.WorksheetFunction.Search("POS 416021XXXXXX5159 SWIGGY", Cell) > 1
Cells(Cell.Row, 8) = "Dining"
Case Application.WorksheetFunction.Search("POS 416021XXXXXX5159 SWIGGY DASH", Cell) > 1
Cells(Cell.Row, 8) = "Groceries"
Case Application.WorksheetFunction.Search("UPI-VODAFONE IDEA LTD-VILPOSKAR", Cell) > 1
Cells(Cell.Row, 8) = "Mobile Bill"
Case Application.WorksheetFunction.Search("UPI-SS LIQOURS", Cell) > 1
Cells(Cell.Row, 8) = "Alcohol"
Case VBA.Left(Cell.Value, 3) = "REV"
Cells(Cell.Row, 8) = "Refunds"
Case Else
Cells(Cell.Row, 8) = ""
End Select
Next Cell
End Sub
这不需要 VBA,如果没有必要,我建议不要 VBA。假设您有这样的数据设置:
B 列是您要搜索的文本,H 列是您希望输出的位置(以匹配代码的作用)。我在 M 和 N 列中创建了一个 table(尽管它可以在任何地方,甚至在不同的 sheet 上),其中包含您要查找的内容以及应该匹配的内容 return。
在单元格 H2 中复制下来的是这个公式:
=IF(LEFT(B2,3)="REV","Refund",IFERROR(INDEX($N:$N,MATCH(999,SEARCH($M:$M,B2))),""))
如果绝对必须是 VBA,那么我建议对 Like
运算符使用 Select Case True
,如下所示:
Sub Lookup_Category()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.ActiveSheet
Dim rNarration As Range: Set rNarration = ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp))
If rNarration.Row < 2 Then Exit Sub 'No data in range
Dim rCell As Range, rTarget As Range, sValue As String
For Each rCell In rNarration.Cells
Set rTarget = ws.Cells(rCell.Row, 8)
sValue = UCase(rCell.Value)
Select Case True
Case sValue Like "*LICIOUS*": rTarget.Value = "Licious"
Case sValue Like "*BILLDESK.ELECTRICITY*": rTarget.Value = "Electricity"
Case sValue Like "*PLAYSTATIONNETWO*": rTarget.Value = "PlayStation"
Case sValue Like "*NEFT CR*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IMPS*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IB FUNDS TRANSFER CR*": rTarget.Value = "Inbound Transfers"
Case sValue Like "*IB BILLPAY DR-HDFCYA-463917XXXXXX5057*": rTarget.Value = "Credit Card Payment"
Case sValue Like "*POS 416021XXXXXX5159 SWIGGY DASH*": rTarget.Value = "Groceries"
Case sValue Like "*POS 416021XXXXXX5159 SWIGGY*": rTarget.Value = "Dining"
Case sValue Like "*UPI-VODAFONE IDEA LTD-VILPOSKAR*": rTarget.Value = "Mobile Bill"
Case sValue Like "*UPI-SS LIQOURS*": rTarget.Value = "Alcohol"
Case Left(sValue, 3) = "REV": rTarget.Value = "Refunds"
Case Else: rTarget.ClearContents
End Select
Next rCell
End Sub