2013-08-23 75 views
0

我有一個宏選擇一個特定範圍,然後在其上找到數字「0」。 我想知道在我選擇的範圍內出現了多少次「0」,以便我可以創建一個等於該數字的變量。我如何設置一個變量等於查找返回匹配查詢的次數?確定在給定範圍內「查找」的匹配數

ActiveCell.Select 
Selection.Offset(0, 1).Select 
item = ActiveCell.Value 
Sheets("Lights").Select 
Rows(3).Select 
Selection.Find(What:=item, After:=ActiveCell, LookIn:=xlValues, LookAt:= _ 
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
    , SearchFormat:=False).Select 
    q = ActiveCell.row() 
    z = ActiveCell.Column() 
    Range(Cells(q, z), Cells(72, z)).Select 
    Selection.Find(What:="0", After:=ActiveCell, LookIn:=xlValues, LookAt:= _ 
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
    , SearchFormat:=False).Activate 

因此,如果有0 7實例的範圍內,那麼我想設置變量m = 7

回答

2
m = WorksheetFunction.CountIf(Selection, 0) 

或部分匹配:

m = WorksheetFunction.CountIf(Selection, "*0*")