QP School

Full Version: Concatenate data in columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub ConcatenateColumns()
    Dim lastRow As Long
    Dim i As Long

    'Find the last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    'Loop through each row and concatenate values from columns A and B
    For i = 1 To lastRow
        Cells(i, "C").Value = Cells(i, "A").Value & " " & Cells(i, "B").Value
    Next i
End Sub

Explanation:

The code defines a subroutine named ConcatenateColumns.
It declares variables for the last row with data (lastRow) and a loop counter (i).
The lastRow variable is determined using Cells(Rows.Count, "A").End(xlUp).Row, which finds the last row with data in column A.
The For loop iterates through each row from 1 to lastRow.
In each iteration, the values from columns A and B are concatenated with a space (" ") in between, and the result is placed in column C using Cells(i, "C").Value.