need help using a for loop to concatenate cells

Learner99

New Member
Joined
Apr 7, 2014
Messages
21
Hi everyone.
I'm trying to concatenate a few cells but seem to be missing the mark with my code.
Can you show me where I went wrong.
Thank you

Code:
Dim counter As Long
Dim b As Range
Dim e As Range
Dim g As Range

 Range("E1").Select                             'Set the range for the loop
    Selection.End(xlDown).Select

    Set b = Range("B1:" & Selection.Address)   'destination

    Set e = Range("E1:" & Selection.Address)   'name Part1
    Set g = Range("F1:" & Selection.Address)   'name Part2
    
    For n = 1 To e.Rows.Count                   'This is where you set the column to sort on
       b.Cells(n, 1) = e.Cells(n, 1) &" " &g.Cells(n, 1)    ' <--Trying to get this to concatenate
     Next n
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi everyone.
I'm trying to concatenate a few cells but seem to be missing the mark with my code.
Can you show me where I went wrong.
Thank you

Code:
Dim counter As Long
Dim b As Range
Dim e As Range
Dim g As Range

 Range("E1").Select                             'Set the range for the loop
    Selection.End(xlDown).Select

    Set b = Range("B1:" & Selection.Address)   'destination

    Set e = Range("E1:" & Selection.Address)   'name Part1
    Set g = Range("F1:" & Selection.Address)   'name Part2
    
    For n = 1 To e.Rows.Count                   'This is where you set the column to sort on
       b.Cells(n, 1) = e.Cells(n, 1) &" " &g.Cells(n, 1)    ' <--Trying to get this to concatenate
     Next n

Add a little value to your work:
Code:
For n = 1 To e.Rows.Count
       b.Cells(n, 1) = e.Cells(n, 1).Value & " " & g.Cells(n, 1).Value
Next n
 
Upvote 0
Maybe this will help you to sort it out.
1.
Code:
Range("E1").Select                             
    Selection.End(xlDown).Select
If there are blank cells in column E before the last cell with data, the selected cell will be in the row before the blank cell. Otherwise the last cell in the range will be the last cell with data in column e. For reference, this will be 'lastcell'.
2.
Code:
Set b = Range("B1:" & Selection.Address)
This range would be B1:E & lastcell
3.
Code:
Set e = Range("E1:" & Selection.Address)
This range would be E1:E & lastcell
4.
Code:
Set g = Range("F1:" & Selection.Address)
This Range would be E1:F & lastcell

So, this statement:
Code:
b.Cells(n, 1) = e.Cells(n, 1) &" " &g.Cells(n, 1) 
[/Code}
First iteration Translates to:
B1 = E1 & " " & E1
And it would follow right on down with 2, 3, 4... all concatenating E to E.

Try substituting this:
[Code]
Range("E1").Select                             'Set the range for the loop
    Selection.End(xlDown).Select
    Set b = Range("B1:" & Selection.Address)   'destination
    Set e = Range("E1:" & Selection.Address)   'name Part1
    Set g = Range("F1:" & Selection.Address)   'name Part2
With this:
Code:
Dim lr As Long
lr = Range("E" & Rows.count).End(xlUP) 'Gets last cell with data in column E.
Set b = Range("B1:B" & lr) 'column B only
Set e = Range("E1:E" & lr) 'Column E only
Set g = Range("F1:F" & lr) 'Column F only
'Then add the modified statement
For n = 1 To e.Rows.Count
       b.Cells(n, 1) = e.Cells(n, 1).Value & " " & g.Cells(n, 1).Value
Next n
 
Last edited:
Upvote 0
Thank you for the great explaination and the code assistance.
I tested your suggestion and got a type-missmatch.
 
Upvote 0
Hi everyone I got my answer. Thank you all for your help.
Here is the route I took.
Code:
Dim lcell As Long
For lcell = 1 To Range("E" & Rows.Count).End(xlUp).Row
    Range("B" & lcell).Value = Range("E" & lcell).Value & " " & Range("F" & lcell).Value
Next lcell
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,844
Members
449,471
Latest member
lachbee

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