0

我有一个宏,它选择一个特定的范围,然后会在上面找到数字“0”。我想知道“0”在我选择的范围内出现了多少次,以便我可以创建一个等于该数字的变量。如何将变量设置为等于 find 返回查询匹配项的次数?

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

因此,如果范围内有 7 个 0 实例,那么我想设置变量 m= 7

4

1 回答 1

2
m = WorksheetFunction.CountIf(Selection, 0)

或部分匹配:

m = WorksheetFunction.CountIf(Selection, "*0*")
于 2013-08-23T17:58:37.817 回答