我想将一行单元格中的所有唯一不同值合并到一个单元格中。我想忽略空白单元格。例如,如果我在一行单元格中有 ABA BLANK C,结果将产生 A、B、C,但我希望它产生 A、B、C 并忽略空白单元格。
我尝试了下面的代码,但它为空格添加了一个额外的逗号。有没有办法修复此代码以解决空格问题。
Function CombineUnique(xRg As Range, xChar As String) As String
Dim xCell As Range
Dim xDic As Object
Set xDic = CreateObject("Scripting.Dictionary")
For Each xCell In xRg
xDic(xCell.Value) = Empty
Next
CombineUnique = Join$(xDic.Keys, xChar)
Set xDic = Nothing
End Function
谢谢
答案1
将您的 For Each 循环更改为:
If xCell.Value <> "" Then
xDic(xCell.Value) = Empty
End If
你的职责是:
Function CombineUnique(xRg As Range, xChar As String) As String
Dim xCell As Range
Dim xDic As Object
Set xDic = CreateObject("Scripting.Dictionary")
For Each xCell In xRg
'Checks if the cell is blank/empty
If xCell.Value <> "" Then
xDic(xCell.Value) = Empty
End If
Next
CombineUnique = Join$(xDic.Keys, xChar)
Set xDic = Nothing
End Function