Excel 用 : 和 ; 组合多列每个值对之间

Excel combind multiple columns with : and ; between each value pair

excel 这样的公式

=$C&":"&C2&";"&$D&":"&D2&";"&$E&E2

结果

US:2.27;AU:2.05;BR2.95

我想做一个从 C 列到 W 列的长公式。如何加快编写公式或使用 VBA?

     C      D       E                F
1    US     AU      BR     combined_countryshipping
2    2.27   2.05    2.95   US:2.27;AU:2.05;BR2.95

这只是3个国家的例子。我有 40-50 个国家可以合并。

你可以试一试...

Function CustomConcatenate(ByVal Rng As Range) As String
Dim cell As Range
Dim str As String
For Each cell In Rng
    If cell.Row = Rng.Cells(1).Row Then
        If str = "" Then
            str = cell & ":" & cell.Offset(1, 0)
        Else
            str = str & ";" & cell & ":" & cell.Offset(1, 0)
        End If
    End If
Next cell
CustomConcatenate = str
End Function

然后像这样在 sheet 上尝试...

=CustomConcatenate(C1:W2)

如果您有 Office 365 Excel,那么您可以在数组公式中使用 TEXTJOIN:

将其放入 F2:

=TEXTJOIN(";",TRUE,$C:$E&":"&C2:E2)

作为数组公式,退出编辑模式时需要用Ctrl-Shift-Enter确认,而不是Enter。如果操作正确,那么 Excel 将在公式周围放置 {}

输入F2。按 Ctrl-Shift-Enter 然后 copy/drag 向下。


如果您有时在值中有空白,并且想在输出为空白时跳过国家/地区,请使用此数组公式:

=TEXTJOIN(";",TRUE,IF(C2:E2<>"",$C:$E&":"&C2:E2,""))

这将跳过所有具有空白值的国家/地区。


如果您没有 TEXTJOIN,您可以将其放入工作簿附带的模块中并使用上述公式:

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function