Excel将多列组合在一起:和;在每个价值对之间

时间:2017-09-24 17:00:18

标签: excel excel-vba formula vba

excel这样的公式

=$C$1&":"&C2&";"&$D$1&":"&D2&";"&$E$1&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个国家要结合。

2 个答案:

答案 0 :(得分:2)

你可以尝试一下......

$content = file_get_contents(__DIR__ . '/../views/main.vue');

然后像这样在表格上试试......

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

答案 1 :(得分:2)

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

把它放在F2:

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

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

加入F2。按Ctrl-Shift-Enter然后复制/向下拖动长度。

enter image description here

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

=TEXTJOIN(";",TRUE,IF(C2:E2<>"",$C$1:$E$1&":"&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