连接同时忽略重复值
Concatenation while ignoring duplicate values
我想将一系列单元格连接成一行,除非前一个单元格包含与下一列中的单元格相同的值,并在它们之间添加破折号。例如:
单元格 A2 = 1234567,单元格 B2 = 1234567,单元格 C2 = 9845666 和单元格 D2 = 5521472。
因为B2和A2一样,我想跳过它继续前进。
我希望输出为“1234567-9845666-5521472”,忽略重复值并从本质上根据行内单元格范围内的每个唯一值创建一个唯一键。最多七个单元格可能有重复值。
您可以通过混合使用 IF
和 COUNTIF
来获得该结果,或者您可以使用用户定义的函数(如附件)。将它放在一个模块中,然后在您的工作表中使用 =concatplus(A2:G2,"-",true)
调用它
Public Function concatPlus(rng As Range, Optional sep As String = "", Optional noDup As Boolean = False, Optional skipEmpty As Boolean = False) As String
'concatenates a range with the specified separator
Dim cl As Range, strTemp As String
If noDup Then
Dim newCol As New Collection
On Error Resume Next
For Each cl In rng.Cells
If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then _
newCol.Add cl.Text, cl.Text
Next
For i = 0 To newCol.Count
strTemp = strTemp & newCol(i) & sep
Next
Else
For Each cl In rng.Cells
If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then _
strTemp = strTemp & cl.Text & sep
Next
End If
concatPlus = Left(strTemp, Len(strTemp) - Len(sep))
End Function
这有帮助吗?
=(if(A2=B2,"","A2&" - ""))&(if(or(B2<>A2)&B2<>"",B2&" - ","" ))&(如果(C2<>B2&C2<>"",C2&" - ",""))&(如果(D2<>C2&D2<>"",D2&" - ",""))&(如果( E2<>D2&"",E2&" - ",""))&(if(F2<>E2&F2<>"",F2&" - ",""))
我想将一系列单元格连接成一行,除非前一个单元格包含与下一列中的单元格相同的值,并在它们之间添加破折号。例如:
单元格 A2 = 1234567,单元格 B2 = 1234567,单元格 C2 = 9845666 和单元格 D2 = 5521472。
因为B2和A2一样,我想跳过它继续前进。
我希望输出为“1234567-9845666-5521472”,忽略重复值并从本质上根据行内单元格范围内的每个唯一值创建一个唯一键。最多七个单元格可能有重复值。
您可以通过混合使用 IF
和 COUNTIF
来获得该结果,或者您可以使用用户定义的函数(如附件)。将它放在一个模块中,然后在您的工作表中使用 =concatplus(A2:G2,"-",true)
Public Function concatPlus(rng As Range, Optional sep As String = "", Optional noDup As Boolean = False, Optional skipEmpty As Boolean = False) As String
'concatenates a range with the specified separator
Dim cl As Range, strTemp As String
If noDup Then
Dim newCol As New Collection
On Error Resume Next
For Each cl In rng.Cells
If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then _
newCol.Add cl.Text, cl.Text
Next
For i = 0 To newCol.Count
strTemp = strTemp & newCol(i) & sep
Next
Else
For Each cl In rng.Cells
If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then _
strTemp = strTemp & cl.Text & sep
Next
End If
concatPlus = Left(strTemp, Len(strTemp) - Len(sep))
End Function
这有帮助吗?
=(if(A2=B2,"","A2&" - ""))&(if(or(B2<>A2)&B2<>"",B2&" - ","" ))&(如果(C2<>B2&C2<>"",C2&" - ",""))&(如果(D2<>C2&D2<>"",D2&" - ",""))&(如果( E2<>D2&"",E2&" - ",""))&(if(F2<>E2&F2<>"",F2&" - ",""))