用逗号连接 If Not Null 列表
Concatenate If Not Null List With Commas
在 Excel 中,我需要将所有其他单元格连接成一个 "master" 单元格。我在 =SUBSTITUTE(TRIM(G2 & " " & BC2 & " " & BE2 & " " & BG2), " ", ", ")
之前使用过这个公式来征服,但是当我连接的数据中有一个逗号时,这会改变数据。
我需要连接的单元格范围是从 G2 一直到 BG2 的所有其他单元格。处理涉及逗号列表的串联时,最好的做法是什么?
编辑
我对改变数据的意思是这个
S223 - Pills, S2323 - Patterns - Backstock, 1/Var
用上面的公式变成这样
S223, -, Pills,, S2323, -, Patterns, -, Backstock,, 1/Var
不久前,我在网上找到了这段代码,因为我的个人项目需要它。它的作用是获取一个字符串并用指定字符替换每个 "unallowed" 值。在你的情况下,我会想象你允许除“,”之外的每个字符并将其替换为“”或“”。这样,A,-,B,-,C, 就变成了 A - B - C
Function cleanString(text As String) As String
Dim output As String
Dim c
For i = 1 To Len(text)
c = Mid(text, i, 1)
If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z" Or c = " " Or c = "-" Or c = "é" Or c = "É" Or c = "_") Then ' <=list of allowed values in a string
output = output & c
Else
output = output & " " '<= what unallowed values gets replaced by
End If
Next
cleanString = output
End Function
希望这对您有所帮助,我考虑了 VBA,因为您在问题中添加了标签。
来自 answers.microsoft.com 的以下 UDF 应该是您想要的
Function TEXTJOIN(delimiter As String, ignore_empty As String, ParamArray textn() As Variant) As String
Dim i As Long
For i = LBound(textn) To UBound(textn) - 1
If Len(textn(i)) = 0 Then
If Not ignore_empty = True Then
TEXTJOIN = TEXTJOIN & textn(i) & delimiter
End If
Else
TEXTJOIN = TEXTJOIN & textn(i) & delimiter
End If
Next
TEXTJOIN = TEXTJOIN & textn(UBound(textn))
End Function
使用 UDF:
公式:
=TEXTJOIN(", ",TRUE,IF(MOD(COLUMN(G2:BG2),2)=1,G2:BG2,""))
作为数组公式,需要使用 Ctrl-Shift-Enter 确认。
在 Excel 中,我需要将所有其他单元格连接成一个 "master" 单元格。我在 =SUBSTITUTE(TRIM(G2 & " " & BC2 & " " & BE2 & " " & BG2), " ", ", ")
之前使用过这个公式来征服,但是当我连接的数据中有一个逗号时,这会改变数据。
我需要连接的单元格范围是从 G2 一直到 BG2 的所有其他单元格。处理涉及逗号列表的串联时,最好的做法是什么?
编辑
我对改变数据的意思是这个
S223 - Pills, S2323 - Patterns - Backstock, 1/Var
用上面的公式变成这样
S223, -, Pills,, S2323, -, Patterns, -, Backstock,, 1/Var
不久前,我在网上找到了这段代码,因为我的个人项目需要它。它的作用是获取一个字符串并用指定字符替换每个 "unallowed" 值。在你的情况下,我会想象你允许除“,”之外的每个字符并将其替换为“”或“”。这样,A,-,B,-,C, 就变成了 A - B - C
Function cleanString(text As String) As String
Dim output As String
Dim c
For i = 1 To Len(text)
c = Mid(text, i, 1)
If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z" Or c = " " Or c = "-" Or c = "é" Or c = "É" Or c = "_") Then ' <=list of allowed values in a string
output = output & c
Else
output = output & " " '<= what unallowed values gets replaced by
End If
Next
cleanString = output
End Function
希望这对您有所帮助,我考虑了 VBA,因为您在问题中添加了标签。
来自 answers.microsoft.com 的以下 UDF 应该是您想要的
Function TEXTJOIN(delimiter As String, ignore_empty As String, ParamArray textn() As Variant) As String
Dim i As Long
For i = LBound(textn) To UBound(textn) - 1
If Len(textn(i)) = 0 Then
If Not ignore_empty = True Then
TEXTJOIN = TEXTJOIN & textn(i) & delimiter
End If
Else
TEXTJOIN = TEXTJOIN & textn(i) & delimiter
End If
Next
TEXTJOIN = TEXTJOIN & textn(UBound(textn))
End Function
使用 UDF:
公式:
=TEXTJOIN(", ",TRUE,IF(MOD(COLUMN(G2:BG2),2)=1,G2:BG2,""))
作为数组公式,需要使用 Ctrl-Shift-Enter 确认。