如果不是空列表,用逗号连接

在Excel中,我需要将每个其他单元格连接成一个“主”单元格。 我已经用这个公式来征服之前=SUBSTITUTE(TRIM(G2 & " " & BC2 & " " & BE2 & " " & BG2), " ", ", ")但是当我连接的数据有这个改变数据在它的逗号。

我需要连接的单元格范围是从G2到BG2的所有其他单元格。 什么是最好的行动,使这发生处理连接,涉及逗号列表?

编辑
我的意思是改变数据是这个

 S223 - Pills, S2323 - Patterns - Backstock, 1/Var 

成为这个与上面的公式

 S223, -, Pills,, S2323, -, Patterns, -, Backstock,, 1/Var 

使用UDF: 具有多个条件的VLOOKUP在一个单元格中返回值

公式:

  =TEXTJOIN(", ",TRUE,IF(MOD(COLUMN(G2:BG2),2)=1,G2:BG2,"")) 

作为一个数组公式需要用Ctrl-Shift-Enter确认。

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 

很久以前,我在网上find这个代码,因为我需要一个我的个人项目。 它所做的是接收一个string,并用指定的字符replace每个“不允许的”值。 在你的情况下,我会想象你允许除“,”以外的每个字符,并replace为“”或“”。 这样,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。