VBA连接两个范围

时间:2018-07-10 09:58:28

标签: excel-vba

我正在尝试使用VBA在excel中连接两个范围,因此range1中的每个单元格都将range2中的所有单元格串联起来,直到单元格A为空。请看下面:

Range1(column A): Range2(column B):
50703, 50702      52797, 52848

Concatenate(column C):
50703-52797, 50703-52848, 50702-52797, 50702-52848

3 个答案:

答案 0 :(得分:1)

这会将A列和B列的所有组合插入C列,并用连字符将它们连接起来:

Sub combinations()

    Dim i As Long, j As Long, n As Long
    Dim valsColA As Variant, valsColB As Variant

    With ThisWorkbook.Sheets("Combinations") ' change sheet name, if necessary

        valsColA = .Range(.Cells(1, 1), .Range("A1").End(xlDown)).Value
        valsColB = .Range(.Cells(1, 2), .Range("B1").End(xlDown)).Value

        For i = LBound(valsColA) To UBound(valsColA)
            For j = LBound(valsColB) To UBound(valsColB)
                n = n + 1
                .Cells(n, 3).Value = valsColA(i, 1) & "-" & valsColB(j, 1)
            Next j
        Next i

    End With
End Sub

enter image description here

答案 1 :(得分:0)

这是我想出的,尽管@ Miqi180首先到达那里:

{
    "require": {
        "intervention/image": "^2.3",
        "10up/wp_mock": "0.3.0",
        "phpunit/phpunit": "^7.2" // this is the new line
    }
}

答案 2 :(得分:0)

尝试此代码

Sub Test()
Dim rng         As Range

Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
rng.Offset(, 2).Value = Evaluate("If(Row(1:" & rng.Rows.Count & ")," & rng.Address(, , , True) & " & " & "-" & rng.Offset(, 1).Address(, , , True) & ")")
End Sub