如何用分隔符连接多个单元格(仅当单元格不为空时)?
How to concatenate multiple cells (only if cell is not blank) with delimiter?
我正在尝试将 6 个单元格与每个单元格之间的分隔符“,”连接起来,但同时忽略空白单元格。我用这个问题来帮助我:How to Concatenate multiple columns if not empty。
问题:
我 运行 遇到分隔符出现在连接文本末尾的问题。所以我用 LEFT 和 LEN 去掉了多余的字符。有没有办法在 VBA 中解决此问题而不在公式中使用 LEFT 和 LEN?
VBA代码:
Function Concat(ConcatArea As Range) As String
For Each x In ConcatArea: xx = IIf(x = "", xx & "", xx & x & ", "): Next
Concat = Left(xx, Len(xx) - 1)
End Function
公式:
=LEFT(Concat(Temp[@[En00]:[En05]]),LEN(Concat(Temp[@[En00]:[En05]]))-1)
解法:
在 @Andreas and Alun Rowe's resource 的帮助下,我能够使用不同的 UDF。 UDF 似乎模拟了 TEXTJOIN 函数(仅在 Office 365/2019 中可用):
Function IMPLODE(Rng As Range, Sep As String)
Dim TEMP As String
For Each Cell In Rng
If Cell.Value = "" Then
Else
TEMP = TEMP & Cell.Value & Sep
End If
Next Cell
TEMP = Left(TEMP, Len(TEMP) - Len(Sep))
IMPLODE = TEMP
End Function
您可以使用 PHP 等效的内爆函数作为 UDF。
所有致谢原作者 here.
Function IMPLODE(Rng As Range, Sep As String)
Dim TEMP As String
For Each Cell In Rng
If Cell.Value = "" Then
Else
TEMP = TEMP & Cell.Value & Sep
End If
Next Cell
TEMP = Left(TEMP, Len(TEMP) - Len(Sep))
IMPLODE = TEMP
End Function
我正在尝试将 6 个单元格与每个单元格之间的分隔符“,”连接起来,但同时忽略空白单元格。我用这个问题来帮助我:How to Concatenate multiple columns if not empty。
问题:
我 运行 遇到分隔符出现在连接文本末尾的问题。所以我用 LEFT 和 LEN 去掉了多余的字符。有没有办法在 VBA 中解决此问题而不在公式中使用 LEFT 和 LEN?
VBA代码:
Function Concat(ConcatArea As Range) As String
For Each x In ConcatArea: xx = IIf(x = "", xx & "", xx & x & ", "): Next
Concat = Left(xx, Len(xx) - 1)
End Function
公式:
=LEFT(Concat(Temp[@[En00]:[En05]]),LEN(Concat(Temp[@[En00]:[En05]]))-1)
解法:
在 @Andreas and Alun Rowe's resource 的帮助下,我能够使用不同的 UDF。 UDF 似乎模拟了 TEXTJOIN 函数(仅在 Office 365/2019 中可用):
Function IMPLODE(Rng As Range, Sep As String)
Dim TEMP As String
For Each Cell In Rng
If Cell.Value = "" Then
Else
TEMP = TEMP & Cell.Value & Sep
End If
Next Cell
TEMP = Left(TEMP, Len(TEMP) - Len(Sep))
IMPLODE = TEMP
End Function
您可以使用 PHP 等效的内爆函数作为 UDF。
所有致谢原作者 here.
Function IMPLODE(Rng As Range, Sep As String)
Dim TEMP As String
For Each Cell In Rng
If Cell.Value = "" Then
Else
TEMP = TEMP & Cell.Value & Sep
End If
Next Cell
TEMP = Left(TEMP, Len(TEMP) - Len(Sep))
IMPLODE = TEMP
End Function