简体   繁体   English

如何计算细胞之间的差异

[英]How to count differences between Cells

I have VBA which comparing 2 cells.我有 VBA 比较 2 个单元格。 Each cell can contain between 1 and 3 different parameters ant parameters are trimmed by the "," comparison is made by simple double for loop(check code).每个单元格可以包含 1 到 3 个不同的参数Thing what i can't figure it out is that: How to modify code and get number of unique entries, example cell 1 [music, art, science];我无法弄清楚的是:如何修改代码并获取唯一条目的数量,例如单元格 1 [音乐、艺术、科学]; cell 2 [art, music];单元格 2 [艺术,音乐]; When i run my for loops i get 2 matches(which is fine) but how to count number of unique words in this case should be 3.当我运行我的 for 循环时,我得到 2 个匹配项(这很好),但在这种情况下如何计算唯一单词的数量应该是 3。

I have tried to enter this part of code but its not working well num_possible = num_possible + 1我试图输入这部分代码,但它不能正常工作num_possible = num_possible + 1

game_tags_parts = Split(Cells(11, 2), ",")
game_tags_parts_j = Split(Cells(11, j), ",")
   num_matches = 0
   num_possible = 0
For m = LBound(game_tags_parts) To UBound(game_tags_parts)
   num_possible = num_possible + 1
        For n = LBound(game_tags_parts_j) To UBound(game_tags_parts_j)
           If Trim(game_tags_parts(m)) = Trim(game_tags_parts_j(n)) Then
               num_matches = num_matches + 1
           End If
        Next n
Next m

Actual result should be number of unique words used in those cells, in some cases i get 3 matches, example cell 1 [scifi, space, star] cell 2 [star, space, scifi] and its in total 3 matches.实际结果应该是这些单元格中使用的唯一单词的数量,在某些情况下,我得到 3 个匹配项,例如单元格 1 [scifi, space, star] 单元格 2 [star, space, scifi] 总共有 3 个匹配项。 Modification should provide me an number 3 as number of unique words used in both cells.修改应该为我提供一个数字 3 作为两个单元格中使用的唯一词的数量。 Or in this case where i have cell 1 [art, music, science] and cell 2 [scifi, space, star] where program gives me 0 same words and modification should give me a number 6 as unique used words.或者在这种情况下,我有单元格 1 [艺术、音乐、科学] 和单元格 2 [科幻、空间、明星],其中程序给了我 0 个相同的词,而修改应该给我一个数字 6 作为唯一使用的词。

One easy way to get a unique count is to use a Dictionary object:获得唯一计数的一种简单方法是使用字典 object:

game_tags_parts = Split(Cells(11, 2), ",")
game_tags_parts_j = Split(Cells(11, j), ",")

Dim myDict As Object
Set myDict = CreateObject("Scripting.Dictionary")

For Each v In game_tags_parts
    If Not myDict.Exists(v) Then myDict.Add v, v
Next v

For Each v In game_tags_parts_j
    If Not myDict.Exists(v) Then myDict.Add v, v
Next v

MsgBox "unique count: " & myDict.Count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM