Question : Excel VBA - Concatenate cells in every other column in range

The attached file has some columns where I would like to join all the text columns (every second column starting with B) and put the results in a column on Sheet1, for example.
Hard-coded with a formula, it would be pretty simple  =concatenate(b2, " | ", d2, " | ", f2, " | ", h2) with the pipe-symbol " | " dividing each text field.  Or it could just be =b2 & " | " & d2 ... etc.  Then paste the formula down the page.

I cannot figure out how to do this in VBA though.
I think you have to use a For ... Each and count the columns across to End(xlToLeft) then Step 2 for every other column.
Then ...?  each cell is added to the next one until the end of the range. ???
For whatever reason the syntax for the Range and Cells collection (and the logic of counting across and then referencing the variables) does not make sense or work for me.
Thanks for your help!
Attachments:
 
concatenate cells in every other column
 

Answer : Excel VBA - Concatenate cells in every other column in range

Try this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Sub ConcatenateText()
    
    Dim lastRow As Long
    Dim lastCol As Long
    Dim tempLastRow As Long
    Dim i As Long 'Increment Rows
    Dim p As Long 'Increment Columns
    Dim conValue As String
    Dim conSheet As String 'Concatenate Sheet Name
    Dim destSheet As String 'Destination Sheet Name
    
    destSheet = "Sheet1"
    conSheet = "surveyText"

    lastRow = Sheets(conSheet).Range("A" & Rows.Count).End(xlUp).Row
    lastCol = Sheets(conSheet).Range(Columns.Count & ":1").End(xlToRight).Column
    
    For i = 2 To lastRow
        For p = 2 To lastCol Step 2
            conValue = conValue & " | " & Sheets(conSheet).Cells(i, p).Value
        Next
        tempLastRow = Sheets(destSheet).Range("A" & Rows.Count).End(xlUp).Row + 1
        Sheets(destSheet).Cells(tempLastRow, 1).Value = Right(conValue, Len(conValue) - 3)
        conValue = ""
    Next i
    
    MsgBox "Done!"
    
End Sub
Random Solutions  
 
programming4us programming4us