我有一个脚本字典代码,该代码删除了相似的数据。该代码删除了几乎不需要的重复数据。
我有这种形式的数据:
USA Test Green
USA Test Blue
India Test Red
India Test White
我希望将数据转换为这种形式
USA:
Test Green
Test Blue
India:
Test Red
Test White
但是,我用当前代码获得了这些结果
USA:
Test Green
Test Blue
India:
Test Red
'<---- missing last observation
我几乎肯定会发生这种情况,因为我有
If Not InStr(1, countrydict(country), data) > 0
在我的代码中,我尝试删除了代码的几个部分,但未能使其正常工作。
我的代码如下
With ActiveSheet
For iter = 2 To lastrow
country = Trim(.Cells(iter, 1).Value)
data = Trim(.Cells(iter, 2).Value)
If countrydict.Exists(country) Then
If Not InStr(1, countrydict(country), data) > 0 Then ' <---- This is most likely where I have gone wrong
countrydict(country) = countrydict(country) & "|" & data ' an array would work but we can instr a string
End If
Else
countrydict.Add country, data
End If
Next
iter = 2
For Each key In countrydict
.Cells(iter, 1).Value = key & ":"
.Cells(iter, 1).Interior.Color = RGB(204, 219, 235)
.Cells(iter, 1).Font.Size = 11
.Cells(iter, 1).Font.FontStyle = "Arial"
.Cells(iter, 1).Font.Bold = True
iter = iter + 1
For diter = 0 To UBound(Split(countrydict(key), "|"))
.Cells(iter, 1).Value = Split(countrydict(key), "|")(diter)
.Cells(iter, 1).Font.Size = 9
.Cells(iter, 1).Font.FontStyle = "Arial"
iter = iter + 1
Next
Next
.Columns("B").Clear
End With
一如既往,我们非常感谢您的帮助。