我记录了这个简单的SearchFind宏,是否可以引用搜索值找到的单元格数
或
一个真正的错误测试,以便如果Searchfind找到一个具有搜索值的单元格然后做一些事情,那么做一些不同的事情。
谢谢(我希望我很清楚)
Sub Macro2()
Columns("B:B").Select
Cells.Find(What:="MyValue", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase _
:=False, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
End Sub
答案 0 :(得分:2)
试试这个:
Sub Macro2()
Dim rng As Range
With Columns("B:B")
Set rng = .Find(What:="MyValue", LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
'do something if "MyValue" is found
Set rng = .FindNext(rng)
Else
'do something if "MyValue" is NOT found
End If
End With
End Sub
搜索值中找到了多少个单元格
你可以使用:
MsgBox WorksheetFunction.CountIf(Columns("B:B"), "MyValue")
我还建议你限定你的范围Columns("B:B")
,即更好地使用ThisWorkbook.Worksheets("Sheet1").Columns("B:B")
答案 1 :(得分:2)
您可以检查确定是否发现了这样的内容:
Dim FoundRng As Range
'this search step is the same as yours, but assigns the result to a range rather than activating the cell
Set FoundRng = Cells.Find(What:="MyValue", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase _
:=False, SearchFormat:=False)
'check whether or not the range is "nothing"
If FoundRng Is Nothing Then
'the value was NOT found, do something
Else
'the value WAS found, do something else
End If