Excel 2010 VBA. Concatenate 2 columns from a Sheet and paste them in another

∥☆過路亽.° 提交于 2019-12-02 20:38:28

问题


Using Excel 2010, I'm trying to create a script that concatenates two text columns (A and B) from Sheet1 and pastes the result in column A of Sheet2.

The workbook uses an external datasource for loading both columns, so the number of rows is not fixed.

I've tried the following code, but not working. variable lRow is not taking any value.

Sub Concat()
Sheets("Sheet1").Select
Dim lRow As Long
lRow = Range("A" & Rows.count).End(xlUp).Row
For i = 2 To lRow
    ActiveWorkbook.Sheets("Sheet2").Cells(i, 1) = Cells(i, 1) & Cells(i, 2)
Next i

End Sub

What am I doing wrong. Thanks for helping!


回答1:


As to what are you doing wrong, I suggest you use

Sub Concat()
    Sheets("Sheet1").Select
    Dim lRow As Long, i As Long
    Dim rng As Range
    Set rng = Range("A" & Rows.Count).End(xlUp)
    Debug.Print rng.Address(External:=True)
    lRow = rng.Row
    For i = 2 To lRow
        ActiveWorkbook.Sheets("Sheet2").Cells(i, 1) = Cells(i, 1) & Cells(i, 2)
    Next i
End Sub

to see what is going on. I tried exactly what you used and it worked for me (Excel 2010).

Specifying what does "variable lRow is not taking any value" mean would help.

You could also try alternatively

Sub Concat2()
    Sheets("Sheet1").Select
    Dim lRow As Long, i As Long
    Dim rng As Range
    Set rng = Range("A2").End(xlDown)
    Debug.Print rng.Address(External:=True)
    lRow = rng.Row
    For i = 2 To lRow
        ActiveWorkbook.Sheets("Sheet2").Cells(i, 1) = Cells(i, 1) & Cells(i, 2)
    Next i
End Sub

which should give the same result if yo do not have blank cells in the middle of the source column A.




回答2:


I would advise getting out of the .Select method of XL VBA programming in favor of direct addressing that will not leave you hanging with errors.

Sub Concat()
    Dim i As Long, lRow As Long
    With Sheets("Sheet1")
        lRow = .Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To lRow
            Sheets("Sheet2").Cells(i, 1) = .Cells(i, 1) & .Cells(i, 2)
        Next i
    End With
End Sub

Note the periods (aka . or full stop) that prefix .Cells and .Range. These tell .Cells and .Range that they belong to the worksheet referenced in the With ... End With block; in this example that would be Sheets("Sheet1").

If you have a lot of rows to string together you would be better off creating an array of the values from Sheet1 and processing the concatenation in memory. Split off the concatenated values and return them to Sheet2.

Sub concat2()
    Dim c As Long, rws As Long, vCOLab As Variant
    With Sheets("Sheet1")
        rws = .Range("A2:A" & .Cells(Rows.Count, 1).End(xlUp).Row).Rows.Count
        vCOLab = .Range("A2").Resize(rws, 3)
        For c = LBound(vCOLab, 1) To UBound(vCOLab, 1)
            'Debug.Print vCOLab(c, 1) & vCOLab(c, 2)
            vCOLab(c, 3) = vCOLab(c, 1) & vCOLab(c, 2)
        Next c
    End With
    Sheets("Sheet2").Range("A2").Resize(rws, 1) = Application.Index(vCOLab, , 3)
End Sub

When interacting with a worksheet, bulk operations will beat a loop every time; the only question is by how much.



来源:https://stackoverflow.com/questions/19956515/excel-2010-vba-concatenate-2-columns-from-a-sheet-and-paste-them-in-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!