Add text from one cell to the beginning of a range of cells in the same row (if those cells have a value)

DebN

New Member
Joined
Sep 21, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hello I know next to nothing about VBA code and would really appreciate some help.
I would like to know if it is possible to add the text of one cell (for example a name in cell B2) to the beginning of a range of other cells (for example C2 to G2) if those cells have a value entered in them.
I would like to do this for rows 2 to 10, placing the text from the B column of that row at the front of the cells in that same row (cells C-G)

ABCDEFG
Joe123012001200
Mary830845830845845
John845950950950
When I run the macro, I would like it to change to:
JoeJoe1230Joe1200Joe1200
MaryMary830Mary845Mary830Mary845Mary845
JohnJohn845John950John950John950

Thanks so much to anyone willing to help me out.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Just to get the ball rolling. Now I can put together a basic program. Maybe some of the A Students will weigh in and tighten up the program. If you have any questions please let us know

VBA Code:
Sub Join1()
Dim LastRow  As Long
Dim Row1 As Long
Dim Col1 As Long

LastRow = Cells(Rows.Count, "B").End(xlUp).Row

For Row1 = 2 To LastRow

    For Col1 = 3 To 7
        If Cells(Row1, Col1) <> "" Then
            Cells(Row1, Col1) = Cells(Row1, 2) & Cells(Row1, Col1)
        End If
    Next Col1

Next Row1

End Sub

20-09-21 join cells.xlsm
ABCDEFGHI
1
2JoeJoe1230Joe1200Joe1200
3MaryMary830Mary845Mary830Mary845Mary845
4JohnJohn845John950John950John950
5
6When I run the macro, I would like it to change to:
7JoeJoe1230Joe1200Joe1200
8MaryMary830Mary845Mary830Mary845Mary845
9JohnJohn845John950John950John950
Join Text and Numbers
 
Upvote 0
Try
Code:
Sub Maybe_So()
Dim c As Range, cel As Range
    For Each c In Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row)
        For Each cel In c.Offset(, 1).Resize(, Cells(c.Row, Columns.Count).End(xlToLeft).Column - 2).SpecialCells(2)
            cel.Value = c.Value & cel.Value
        Next cel
    Next c
End Sub
 
Upvote 0
Read your requirement again.
Change the B1 to B2 as you want to start at the 2nd row.
If your row10 is a hard number, change that whole line to
Code:
For Each c In Range("B2:B10")
The way it is in Post #3 is more versatile. It doesn't matter how much data is in Column B.
 
Upvote 0
One more way (no loops)...
VBA Code:
Sub test()
  With Range("C1", Cells(Cells(Rows.Count, "B").End(xlUp).Row, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column))
    .Value = Evaluate(Replace("IF(@="""","""",B1:B" & .Rows.Count & "&@)", "@", .Address))
  End With
End Sub
 
Upvote 0
Just to get the ball rolling. Now I can put together a basic program. Maybe some of the A Students will weigh in and tighten up the program. If you have any questions please let us know

VBA Code:
Sub Join1()
Dim LastRow  As Long
Dim Row1 As Long
Dim Col1 As Long

LastRow = Cells(Rows.Count, "B").End(xlUp).Row

For Row1 = 2 To LastRow

    For Col1 = 3 To 7
        If Cells(Row1, Col1) <> "" Then
            Cells(Row1, Col1) = Cells(Row1, 2) & Cells(Row1, Col1)
        End If
    Next Col1

Next Row1

End Sub

20-09-21 join cells.xlsm
ABCDEFGHI
1
2JoeJoe1230Joe1200Joe1200
3MaryMary830Mary845Mary830Mary845Mary845
4JohnJohn845John950John950John950
5
6When I run the macro, I would like it to change to:
7JoeJoe1230Joe1200Joe1200
8MaryMary830Mary845Mary830Mary845Mary845
9JohnJohn845John950John950John950
Join Text and Numbers
Wow, it works! Thanks so much... I can't believe you are so kind (to write it for me).... Thanks!
 
Upvote 0
Read your requirement again.
Change the B1 to B2 as you want to start at the 2nd row.
If your row10 is a hard number, change that whole line to
Code:
For Each c In Range("B2:B10")
The way it is in Post #3 is more versatile. It doesn't matter how much data is in Column B.
You people are amazing... thanks - this worked!
 
Upvote 0
One more way (no loops)...
VBA Code:
Sub test()
  With Range("C1", Cells(Cells(Rows.Count, "B").End(xlUp).Row, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column))
    .Value = Evaluate(Replace("IF(@="""","""",B1:B" & .Rows.Count & "&@)", "@", .Address))
  End With
End Sub
Wow - excellent - thank you, thank you, thank you!.... This code is excellent. Works like a charm.
 
Upvote 0
Thank you for taking the time to let us know that you were satisfied with the suggestions.
Wish all people were that well brought up.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,025
Members
448,939
Latest member
Leon Leenders

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