Lets say I have 10 000 rows with 4 countries and I want to color entire row based on Country. Number of countries might change so I want to keep this dynamic.
Excel File - Unique Country Values. | Country | | ------- | | SWEDEN | | FINLAND | | DENMARK | | JAPAN |
Firstly I do dictionary to get unique country values with code below.
data = ActiveSheet.UsedRange.Columns(1).value
Set dict = CreateObject("Scripting.Dictionary")
For rr = 2 To UBound(data)
dict(data(rr, 1)) = Empty
Next
data = WorksheetFunction.Transpose(dict.Keys())
colors_amount = dict.Count
Then I want to generate random color for each country.
Set dict_color = CreateObject("Scripting.Dictionary")
For k = 1 To colors_amount
myRnd_1 = Int(2 + Rnd * (255 - 0 + 1))
myRnd_2 = Int(2 + Rnd * (255 - 0 + 1))
myRnd_3 = Int(2 + Rnd * (255 - 0 + 1))
color = myRnd_1 & "," & myRnd_2 & "," & myRnd_3
dict_color.Add Key:=color, Item:=color
Next
data_color = WorksheetFunction.Transpose(dict_color.Keys())
Now it is time to create an array which combines country and color.
For k = 0 To colors_amount - 1
varArray(k, 0) = data(k + 1, 1)
varArray(k, 1) = data_color(k + 1, 1)
Next k
And now crucial part, making loop which assigns color to entire row based on country I have no idea how to get proper color value based on Kom Value, below description what I want to do
For Each Kom In Range("A2:A" & lastrow)
'Lets Say Kom Value is Japan so I want to take from array particular RGB Color code and put it on entire row
'I want to connect to array and do VLOOKUP how can I do it ?
Next Kom
Do you have some ideas ?
3条答案
按热度按时间qzwqbdag1#
问题解决了。我做了一个额外的数组,最后的循环看起来像这样:
最终循环
nr9pn0ug2#
这可以通过使用单个字典和自动筛选来完成:
tzdcorbm3#
请测试下一个更新的代码。它使用了两个字典,应该很快,甚至对于大范围创建联合范围(作为字典键),以立即在代码结束时着色。它创建
RGB
颜色:请在测试后发送一些反馈