今天我有以下问题:我在 Excel 中有 2 列 x 行(不管多少行),每行都有一个字符串,像这样
A B
Apple Potato
Banana Potato
Apple Potato
Orange Apple
每个字符串都可以出现在两列中。
我需要获得以下结果:
Fruit Occurrencies
Apple 3
Banana 1
Potato 3
Orange 1
现在,我确信有一种方法比我能想到的要快得多,我将不胜感激您能提供的任何帮助。我的解决方案包括将字符串一一存储在一个数组中,每次检查它们是否已经包含在当前一个插槽之前的插槽中,如果没有,也计算它的出现次数。例如,在将所有字符串存储在一个数组中之后(我现在将调用它Fruit()
):
Dim Str() As Variant
Dim Flag As Boolean
For i = LBound(Fruit)+1 to Ubound(Fruit)
Flag = True
For j = i to LBound(Fruit)
If Fruit(i) = Fruit(j) Then
Flag = False
Exit For
End If
Next
If Flag = True Then
Str(k,0) = Fruit(i)
For y = LBound(Fruit) to UBound(Fruit)
if Str(k,0) = Fruit(y) Then Str(k,1) = Str(k,1)+1
Next
k = k+1
End If
Next
这太疯狂了,我知道有一个更简单的解决方案……我就是找不到。