Using VBA to concatenate two columns

Belinda

Board Regular
Joined
Apr 5, 2004
Messages
61
Hello,

I have an Excel table that has a fixed number of columns but the number of rows can vary depending on the data update. The columns have a header row.

I want to use VBA code to concatenate the text contents of Column F and H with an underscore in between and place the contents in Column J.

E.g. Cell F2 has "FC TAB" and Column H2 has "Allegra Plus". The result in Cell J2 would be "FC TAB_Allegra Plus"

Thank you.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

One way;

Code:
Sub Concat()

Dim lRow As Long

lRow = Range("F" & Rows.count).End(xlUp).Row

For i = 2 To lRow
    Cells(i, 10) = Cells(i, 6) & "_" & Cells(i, 8)
Next i

End Sub

Although this can be done with formula;

=F2&"_"&H2 in J2 copied down.
 
Upvote 0
Code:
Sub concatMyData()

For i = 2 To Cells(Rows.Count, "F").End(xlUp).Row
    Cells(i, "J").Value = Cells(i, "F").Value & "_" & Cells(i, "H").Value
Next i

End Sub
 
Upvote 0
Hi Belinda,

Run the following code while on the tab in question:

Code:
Sub Macro1()

Dim lngLastRow As Long
'Uses Column A to set the 'lngLastRow' variable _
(find the last row) - change if required.
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Range("J2").Formula = "=F2 & ""_"" & H2"
    Range("J2").Copy Range("J3:J" & lngLastRow)

End Sub

HTH

Robert
 
Upvote 0
Hi Trebor,

This could be shortened as such;

Code:
Sub Macro1()

Dim lngLastRow As Long
'Uses Column A to set the 'lngLastRow' variable _
(find the last row) - change if required.
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Range("J2:J" & lngLastRow).Formula = "=F2 & ""_"" & H2"

End Sub
 
Upvote 0
Hi Mikey B,

Thanks for the tip - it's really appreciated :)

Kind regards,

Robert
 
Upvote 0
If you want values in the cells, Evaluate(array formula) can be used in place of copy/paste or range.Value = range.Value.
Code:
Sub Macro1()

Dim lngLastRow As Long
'Uses Column A to set the 'lngLastRow' variable _
(find the last row) - change if required.
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
   
    Range("J2:J" & lngLastRow).Value = Evaluate("=F2:F" & lngLastRow & "&""_""&" & "H2:H" & lngLastRow)

End Sub
 
Upvote 0
I was working on this earlier today but got called away. Now that I have returned I see that my suggestion is very similar to Mike's, but decided I would post it anyway. I have used column F to determine the range (rather than column A) and a slightly different construct in the 'Evaluate' section.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ConCat()<br>    <SPAN style="color:#00007F">With</SPAN> Range("F2", Range("F" & Rows.Count).End(xlUp))<br>        .Offset(, 4).Value = Evaluate(.Address & "& ""_"" & " & .Offset(, 2).Address)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thank you all for your help. The different approaches have been useful in making me understand VBA a little better.
 
Upvote 0
I was working on this earlier today but got called away. Now that I have returned I see that my suggestion is very similar to Mike's, but decided I would post it anyway. I have used column F to determine the range (rather than column A) and a slightly different construct in the 'Evaluate' section.


Sub ConCat()
****With Range("F2", Range("F" & Rows.Count).End(xlUp))
********.Offset(, 4).Value = Evaluate(.Address & "& ""_"" & " & .Offset(, 2).Address)
****End With
End Sub

What if column F is blank? How can this be modified to not concatenate anything if column F contains a blank cell?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top