楼主 liuguansky |
一、数组循环判断
- Sub 单击() '数组循环判断
- Dim arr, i&, j&, s&
- arr = Cells(1, 1).CurrentRegion.Value
- s = 0
- For i = 1 To UBound(arr, 1)
- For j = 1 To UBound(arr, 2)
- If arr(i, j) = "正常" Then
- s = s + 1
- End If
- Next j, i
- MsgBox "共有正常个数为:" & s
- End Sub
二、工作表函数- Sub 单击1() '工作表函数
- MsgBox "共有正常个数为:" & Application.WorksheetFunction.CountIf(Cells(1, 1).CurrentRegion, "正常")
- End Sub
三、VBA filter函数- Sub 单击2() 'filter函数
- Dim arr, i&, arrt, s&
- arr = Cells(1, 1).CurrentRegion.Value
- s = 0
- For i = 1 To UBound(arr, 2)
- arrt = Filter(Application.Transpose(Application.Index(arr, , i)), "正常")
- s = s + UBound(arrt) + 1
- Next i
- MsgBox "共有正常个数为:" & s
- End Sub
四、字典
- Sub 单击3() '字典
- Dim arr, i&, j&, dic
- Set dic = CreateObject("scripting.dictionary")
- arr = Cells(1, 1).CurrentRegion.Value
- For i = 1 To UBound(arr, 1)
- For j = 1 To UBound(arr, 2)
- If arr(i, j) <> "" Then
- If dic.exists(arr(i, j)) Then
- dic(arr(i, j)) = dic(arr(i, j)) + 1
- Else: dic.Add arr(i, j), 1
- End If
- End If
- Next j, i
- If dic.exists("正常") Then
- MsgBox "共有正常个数为:" & dic("正常")
- Else: MsgBox "未找到正常记录"
- End If
- Set dic = Nothing
- End Sub
桌面.rar
欢迎大家补充。 |