VBA 宏计算单元格中出现次数最多的内容及出现次数
问题:如何计算某一列单元格中出现次数最多的内容,以及这些内容出现的次数,VBA如何返回 一列中 计数次数最多 或者最少的项
Sub test()
Dim r%, i%, imax&, guige$
Dim arr, brr
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Worksheets("sheet1")
r = .Cells(.Rows.Count, 1).End(xlUp).Row
arr = .Range("a2:a" & r)
For i = 1 To UBound(arr)
d(arr(i, 1)) = d(arr(i, 1)) + 1
If d(arr(i, 1)) > imax Then
imax = d(arr(i, 1)): guige = arr(i, 1)
End If
Next
MsgBox "数量最多的是" & guige & ",数量共有" & imax
End With
End Sub
sheet1 改成你实际单元格
arr = .Range("a2:a" & r)
a2:a改成你实际的起始单元格a2是其实单元格第一个格子,a是列
Sub test()
Dim r%, i%, imax&, guige$
Dim arr, brr
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Worksheets("sheet1")
r = .Cells(.Rows.Count, 1).End(xlUp).Row
arr = .Range("a2:a" & r)
For i = 1 To UBound(arr)
d(arr(i, 1)) = d(arr(i, 1)) + 1
If d(arr(i, 1)) > imax Then
imax = d(arr(i, 1)): guige = arr(i, 1)
End If
Next
MsgBox "数量最多的是" & guige & ",数量共有" & imax
End With
End Sub
sheet1 改成你实际单元格
arr = .Range("a2:a" & r)
a2:a改成你实际的起始单元格a2是其实单元格第一个格子,a是列
顶(0)
踩(0)
- 最新评论