我的代码有问题。如果可以正常工作,它将扫描一列,如果找到特定的关键字("Costco", "Dominos", etc
),则它将在相邻的单元格中输出。 但是,一旦遇到没有第一个关键字(“ Costco”)的单元格,就会出现错误提示。如果有可能修复此代码,我将不胜感激。
否则,我想保留Case
结构,因为我可能会添加更多关键字,并且“搜索”不区分大小写。
以下是我的代码:
Sub FindAndOutput()
Dim Col As Range
Dim FirstRow As Range
Dim rng As Range
Dim WorkRng As Range
Set Col = Application.InputBox("Select Column", "Obtain Object Range", Type:=8)
Set FirstRow = Application.InputBox("Select FIrst Row", "Obtain Object Range", Type:=8)
Set WorkRng = Range(Cells(FirstRow.Row, Col.Column), Cells(Cells(Rows.Count, Col.Column).End(xlUp).Row, Col.Column))
For Each rng In WorkRng
Select Case True
Case rng.Find("Costco") <> ""
Cells(rng.Row, rng.Column + 1) = "Costco"
Case rng.Find("Dominos") <> ""
Cells(rng.Row, rng.Column + 1) = "Dominos"
Case Else
Cells(rng.Row, rng.Column).Interior.Color = 12309
End Select
Next
End Sub
以及发生的情况的屏幕截图:
答案 0 :(得分:0)
Sub FindAndOutput()
Dim Col As Range
Dim FirstRow As Range
Dim rng As Range
Dim WorkRng As Range
Set Col = Application.InputBox("Select Column", "Obtain Object Range", Type:=8)
Set FirstRow = Application.InputBox("Select FIrst Row", "Obtain Object Range", Type:=8)
Set WorkRng = Range(Cells(FirstRow.Row, Col.Column), Cells(Cells(Rows.Count, Col.Column).End(xlUp).Row, Col.Column))
For Each rng In WorkRng
Select Case True
Case InStr(1, rng.Value, "Costco", 1) > 0
Cells(rng.Row, rng.Column + 1) = "Costco"
'Other Keywords
Case Else
Cells(rng.Row, rng.Column).Interior.Color = 65535
End Select
Next
End Sub