Excel 计算范围内每个唯一字符串的出现次数
我有一个很大的值范围,中间有一些空格,我想知道如何找到所有不同值的总和,每个值的总和都在这个范围内 例如,我有(在A1:D5范围内): 我想让程序吐出:Excel 计算范围内每个唯一字符串的出现次数,excel,excel-2010,vba,Excel,Excel 2010,Vba,我有一个很大的值范围,中间有一些空格,我想知道如何找到所有不同值的总和,每个值的总和都在这个范围内 例如,我有(在A1:D5范围内): 我想让程序吐出: (在范围、msgbox或任何内容中,用户需要记下数字) 我所尝试的: 我尝试使用CountIF函数,但一直无法正确地计算出来。 我有超过800行要测试,所以我希望避免在一个简单的for循环中迭代每一行 奖励积分: (我很乐意回答上述问题,但如果有人也能解决这个问题,我将不胜感激) 有些单元格值由单词的多个实例甚至多个单词组成。 例如,一些单元格
(在范围、msgbox或任何内容中,用户需要记下数字) 我所尝试的:
我尝试使用
CountIF
函数,但一直无法正确地计算出来。我有超过800行要测试,所以我希望避免在一个简单的for循环中迭代每一行 奖励积分:
(我很乐意回答上述问题,但如果有人也能解决这个问题,我将不胜感激)
有些单元格值由单词的多个实例甚至多个单词组成。
例如,一些单元格包含
Low
Low
仅由回车分隔。
本月甚至有一个单元格包含
Low
Low
High
Low
Low
我还想计算单元格内的每个事件,因此上面的单元格将给出输出:
High: 1
Low: 4
尝试.find方法。转到VBA帮助,查找range.find方法以了解更多信息-它还提供了一些代码,您可以轻松修改这些代码。
我建议对每个值使用一个计数器,该计数器会在每次查找时更新。例如:
Dim Low_count As Long
Low_count = 0
With Worksheets(1).Range("a1:a500")
Set c = .Find("Low", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
Low_count = Low_count + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
Dim Low_count尽可能长
低计数=0
带工作表(1)。范围(“a1:a500”)
设置c=.Find(“低”,LookIn:=xlValues)
如果不是,那么c什么都不是
firstAddress=c.地址
做
低计数=低计数+1
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为firstAddress
如果结束
以
尝试.find方法。转到VBA帮助,查找range.find方法以了解更多信息-它还提供了一些代码,您可以轻松修改这些代码。我建议对每个值使用一个计数器,该计数器会在每次查找时更新。例如:
Dim Low_count As Long
Low_count = 0
With Worksheets(1).Range("a1:a500")
Set c = .Find("Low", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
Low_count = Low_count + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
Dim Low_count尽可能长
低计数=0
带工作表(1)。范围(“a1:a500”)
设置c=.Find(“低”,LookIn:=xlValues)
如果不是,那么c什么都不是
firstAddress=c.地址
做
低计数=低计数+1
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为firstAddress
如果结束
以
尝试一下:
Sub tgr()
Dim cllUnq As Collection
Dim rngCheck As Range
Dim CheckCell As Range
Dim arrUnq(1 To 65000) As String
Dim arrCount(1 To 65000) As Long
Dim varWord As Variant
Dim MatchIndex As Long
Dim lUnqCount As Long
On Error Resume Next
Set rngCheck = Application.InputBox("Select the cells containing strings to be counted", "Select Range", Selection.Address, Type:=8)
On Error GoTo 0
If rngCheck Is Nothing Then Exit Sub 'Pressed cancel
Set cllUnq = New Collection
For Each CheckCell In rngCheck.Cells
For Each varWord In Split(CheckCell.Text, Chr(10))
If Len(Trim(varWord)) > 0 Then
On Error Resume Next
cllUnq.Add varWord, varWord
On Error GoTo 0
If cllUnq.Count > lUnqCount Then
lUnqCount = cllUnq.Count
arrUnq(lUnqCount) = CStr(varWord)
arrCount(lUnqCount) = 1
Else
MatchIndex = WorksheetFunction.Match(CStr(varWord), arrUnq, 0)
arrCount(MatchIndex) = arrCount(MatchIndex) + 1
End If
End If
Next varWord
Next CheckCell
If lUnqCount > 0 Then
Sheets.Add After:=Sheets(Sheets.Count)
With Range("A1:B1")
.Value = Array("Word", "Count")
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Range("A2").Resize(lUnqCount).Value = Application.Transpose(arrUnq)
Range("B2").Resize(lUnqCount).Value = Application.Transpose(arrCount)
End If
Set cllUnq = Nothing
Set rngCheck = Nothing
Set CheckCell = Nothing
Erase arrUnq
Erase arrCount
End Sub
尝试一下:
Sub tgr()
Dim cllUnq As Collection
Dim rngCheck As Range
Dim CheckCell As Range
Dim arrUnq(1 To 65000) As String
Dim arrCount(1 To 65000) As Long
Dim varWord As Variant
Dim MatchIndex As Long
Dim lUnqCount As Long
On Error Resume Next
Set rngCheck = Application.InputBox("Select the cells containing strings to be counted", "Select Range", Selection.Address, Type:=8)
On Error GoTo 0
If rngCheck Is Nothing Then Exit Sub 'Pressed cancel
Set cllUnq = New Collection
For Each CheckCell In rngCheck.Cells
For Each varWord In Split(CheckCell.Text, Chr(10))
If Len(Trim(varWord)) > 0 Then
On Error Resume Next
cllUnq.Add varWord, varWord
On Error GoTo 0
If cllUnq.Count > lUnqCount Then
lUnqCount = cllUnq.Count
arrUnq(lUnqCount) = CStr(varWord)
arrCount(lUnqCount) = 1
Else
MatchIndex = WorksheetFunction.Match(CStr(varWord), arrUnq, 0)
arrCount(MatchIndex) = arrCount(MatchIndex) + 1
End If
End If
Next varWord
Next CheckCell
If lUnqCount > 0 Then
Sheets.Add After:=Sheets(Sheets.Count)
With Range("A1:B1")
.Value = Array("Word", "Count")
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Range("A2").Resize(lUnqCount).Value = Application.Transpose(arrUnq)
Range("B2").Resize(lUnqCount).Value = Application.Transpose(arrCount)
End If
Set cllUnq = Nothing
Set rngCheck = Nothing
Set CheckCell = Nothing
Erase arrUnq
Erase arrCount
End Sub
我对此感到厌倦,但我不确定如何初始化
firstAddress
Dim firstAddress As Range
不起作用,导致运行时错误,表示未设置。firstAddress
应该是范围吗?是的,firstAddress可以初始化为范围类型。运行时错误可能是因为您将编译器设置为检查是否声明了所有变量,或者您是否在模块工作表的顶部使用了“显式声明”选项。我对此感到厌倦,但我不确定如何初始化firstAddress
Dim firstAddress As Range
不起作用,导致运行时错误,表示未设置。firstAddress
应该是范围吗?是的,firstAddress可以初始化为范围类型。运行时错误可能是因为您将编译器设置为检查是否声明了所有变量,或者是否在模块工作表的顶部使用了显式声明选项。