Excel 从单元格中获取唯一字符串并更改其颜色
下面的代码给出了由逗号分隔的唯一字符串的计数。现在我想对Excel 从单元格中获取唯一字符串并更改其颜色,excel,vba,Excel,Vba,下面的代码给出了由逗号分隔的唯一字符串的计数。现在我想对E列中列出的每个唯一字符串应用唯一的颜色。我找不到如何从字典集合中获取值 Sub test() Dim rng As Range, delim As String Dim e As Variant Dim s As Variant lr = Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row Set rng = Worksheets("Sheet1").R
E
列中列出的每个唯一字符串应用唯一的颜色。我找不到如何从字典集合中获取值
Sub test()
Dim rng As Range, delim As String
Dim e As Variant
Dim s As Variant
lr = Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Worksheets("Sheet1").Range("C1:C" & lr)
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For Each e In rng.Value
If Trim$(e) <> "" Then
For Each s In Split(e, ",")
If Trim$(s) <> "" Then .Item(Trim$(s)) = Empty
Next s
End If
Next e
MsgBox .Count
End With
End Sub
子测试()
尺寸为量程,尺寸为字符串
作为变体的dime
作为变体的Dim s
lr=工作表(“Sheet1”)。单元格(Rows.Count,3)。结束(xlUp)。行
设置rng=工作表(“表1”)。范围(“C1:C”和lr)
使用CreateObject(“Scripting.Dictionary”)
.CompareMode=1
对于每一个e,单位为rng.值
如果修剪$(e)“,则
对于拆分中的每个s(e,“,”)
如果Trim$(s)“,则.Item(Trim$(s))=空
下一个s
如果结束
下一个e
MsgBox.Count
以
端接头
这将让您开始。代码中的注释解释了它的功能。要设置单元格中文本的字体颜色,请参见
子测试()
尺寸为量程,尺寸为字符串
作为变体的dime
作为变体的Dim s
lr=工作表(“Sheet1”)。单元格(Rows.Count,3)。结束(xlUp)。行
设置rng=工作表(“表1”)。范围(“C1:C”和lr)
使用CreateObject(“Scripting.Dictionary”)
.CompareMode=1
'构建唯一元素的字典。
对于每一个e,单位为rng.值
e=修剪$(e)
如果是“e”,则
如果仪表(e,“,”)则
对于拆分中的每个s(e,“,”)
如果Trim$(s)“,则.Item(Trim$(s))=空
下一个s
ElseIf InStr(e,vbLf)则“您错过了值由换行符分隔的情况。
对于拆分中的每个s(e、vbLf)
如果Trim$(s)“,则.Item(Trim$(s))=空
下一个s
其他的
.项目(e)=空
如果结束
如果结束
下一个e
'可用颜色的数组。
暗淡的颜色(3)
颜色(0)=黑色
颜色(1)=红色
颜色(2)=绿色
颜色(3)=蓝色
'为字典中的每个元素指定颜色,
'字典的值是元素的颜色。
作为整数的Dim i
i=0
对于每个e-In键
项目(e)=颜色(i)
i=(i+1)模4
下一个e
'再次解析rng中的元素,这次设置其基于颜色的
在字典里找到的颜色。
对于每一个e,单位为rng.值
e=修剪$(e)
如果是“e”,则
如果仪表(e,“,”)则
对于拆分中的每个s(e,“,”)
如果修剪$(s)“,则
'将当前单元格中修剪$(s)的颜色设置为.Item(修剪$(s))。
如果结束
下一个s
其他仪表(e、vbLf)则
对于拆分中的每个s(e、vbLf)
如果修剪$(s)“,则
'将当前单元格中修剪$(s)的颜色设置为.Item(修剪$(s))。
如果结束
下一个s
其他的
'将当前单元格中e的颜色设置为.Item(e)。
如果结束
如果结束
下一个e
以
端接头
我用下面的方法解决了这个问题。数据在“E”列中。我设法通过代码获得了“G”列中的唯一数字,然后将这些数字应用到“E”列的数字上
子超链接prs()
尺寸为量程,尺寸为字符串
作为变体的dime
作为变体的Dim s
lr=工作表(“RawData”)。单元格(Rows.Count,5)。结束(xlUp)。行
设置rng=工作表(“原始数据”)。范围(“E2:E”和lr)
范围(“G:G”)。ClearContents
使用CreateObject(“Scripting.Dictionary”)
.CompareMode=1
对于每一个e,单位为rng.值
lg=工作表(“原始数据”)。单元格(Rows.Count,7)。结束(xlUp)。行
如果修剪$(e)“,则
对于拆分中的每个s(e,“,”)
如果Trim$(s)“,则.Item(Trim$(s))=空
单元格(lg+1,7)。值=s
下一个s
如果结束
下一个e
以
列(7).移除的重复列:=数组(1)“删除重复项”
呼叫COlorPRs
端接头
子颜色PRS()https://www.mrexcel.com/forum/excel-questions/1030392-vba-color-format-text-string.html
调暗颜色键作为范围,toColorRange作为范围,tCR作为范围,cK作为范围
设置颜色键=范围(“G2:G13”)
设置为颜色排列=范围(“E1:E1200”)
对于toColorRange'循环中的每个tCR,所有单元格的颜色范围为
如果tCR.值为“”,则跳过空格
对于colorKey中的每个cK,通过color key的所有单元格循环
如果对照值为“”,则跳过空格
Dim foundNum作为整数
foundNum=1
m=cK.Row
做
foundNum=InStr(foundNum、tCR.Value、cK.Value、vbtextcare)
如果foundNum为0,则
如果m=5,则使用“”以避免难以看到的黄色
m=54
ElseIf m=26那么
m=55
其他的
m=m
如果结束
tCR.Characters(开始:=foundNum,长度:=Len(cK.Value)).Font.ColorIndex=m+1
foundNum=foundNum+1
如果结束
循环直到foundNum=0'在所有字符之间保持循环
如果结束
下一个cK
如果结束
下一个tCR
端接头
由于项(Trim$(s))=Empty
,您只向字典添加键,值始终为空。所以,问题是,您想在字典中添加什么作为值?但它似乎返回了正确的唯一字符串数。我不熟悉字典。我只想给E
列中的每个唯一字符串上色。输入在C
和E
列中。是的,因为您使用了一个d的特性
Sub test()
Dim rng As Range, delim As String
Dim e As Variant
Dim s As Variant
lr = Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Worksheets("Sheet1").Range("C1:C" & lr)
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
' Build dictionary of unique elements.
For Each e In rng.Value
e = Trim$(e)
If e <> "" Then
If InStr(e, ",") Then
For Each s In Split(e, ",")
If Trim$(s) <> "" Then .Item(Trim$(s)) = Empty
Next s
ElseIf InStr(e, vbLf) Then ' You missed this case where values are separated by a linefeed.
For Each s In Split(e, vbLf)
If Trim$(s) <> "" Then .Item(Trim$(s)) = Empty
Next s
Else
.Item(e) = Empty
End If
End If
Next e
' Array of available colors.
Dim colors(3)
colors(0) = vbBlack
colors(1) = vbRed
colors(2) = vbGreen
colors(3) = vbBlue
' Assign color to each element in the dictionary,
' the value of the dictionary is the color of the element.
Dim i As Integer
i = 0
For Each e In keys
keys.Item(e) = colors(i)
i = (i + 1) Mod 4
Next e
' Parse again elements in rng, this time setting its color based
' on color found in dictionary.
For Each e In rng.Value
e = Trim$(e)
If e <> "" Then
If InStr(e, ",") Then
For Each s In Split(e, ",")
If Trim$(s) <> "" Then
' Set color of Trim$(s) in current cell to .Item(Trim$(s)).
End If
Next s
ElseIf InStr(e, vbLf) Then
For Each s In Split(e, vbLf)
If Trim$(s) <> "" Then
' Set color of Trim$(s) in current cell to .Item(Trim$(s)).
End If
Next s
Else
' Set color of e in current cell to .Item(e).
End If
End If
Next e
End With
End Sub
Sub HyperlinkPRs()
Dim rng As Range, delim As String
Dim e As Variant
Dim s As Variant
lr = Worksheets("RawData").Cells(Rows.Count, 5).End(xlUp).Row
Set rng = Worksheets("RawData").Range("E2:E" & lr)
Range("G:G").ClearContents
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For Each e In rng.Value
lg = Worksheets("RawData").Cells(Rows.Count, 7).End(xlUp).Row
If Trim$(e) <> "" Then
For Each s In Split(e, ",")
If Trim$(s) <> "" Then .Item(Trim$(s)) = Empty
Cells(lg + 1, 7).Value = s
Next s
End If
Next e
End With
Columns(7).RemoveDuplicates Columns:=Array(1) '''delete duplicates
Call COlorPRs
End Sub
Sub COlorPRs() ''' https://www.mrexcel.com/forum/excel-questions/1030392-vba-color-format-text-string.html
Dim colorKey As Range, toColorRange As Range, tCR As Range, cK As Range
Set colorKey = Range("G2:G13")
Set toColorRange = Range("E1:E1200")
For Each tCR In toColorRange 'loop though all cells of to be color range
If tCR.Value <> "" Then 'skip blanks
For Each cK In colorKey ' loop through all cells of color key
If cK.Value <> "" Then ' skip blanks
Dim foundNum As Integer
foundNum = 1
m = cK.Row
Do
foundNum = InStr(foundNum, tCR.Value, cK.Value, vbTextCompare)
If foundNum <> 0 Then
If m = 5 Then ''' To avoid yelloish color which is hard to see
m = 54
ElseIf m = 26 Then
m = 55
Else
m = m
End If
tCR.Characters(Start:=foundNum, Length:=Len(cK.Value)).Font.ColorIndex = m + 1
foundNum = foundNum + 1
End If
Loop Until foundNum = 0 'keep looping though all characters
End If
Next cK
End If
Next tCR
End Sub