VBA Concatenation script

Jon_London

New Member
Joined
Jan 24, 2019
Messages
12
Office Version
  1. 365
Hello everyone,

I'm VERY new to VBA and am trying to achieve the following outcome:

I would like a range of cells (e.g. E1:E5) to be concatenated with CP-0001;, CP-0002; and CP-0003;. So, if the contents of cell E1 is 12345 I would like the output to be CP-0001;12345, CP-0002;12345 and CP-0003;12345 on three separate rows in my spreadsheet. The code should then look at cell E2 and repeat the process.

I've tried my very rudimentary code below, but it obviously doesn't work! Could anyone please help me?

Sub vba_concatenate()

Range("A1,A100") = "CP-0001;" & Range("E1,E100")
Range("A1,A100") = "CP-0002;" & Range("E1,E100")
Range("A1,A100") = "CP-0003;" & Range("E1,E100")

End Sub

Many thanks
Jon
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this

VBA Code:
Sub vba_concatenate()

With Range("A1:A100")
.FormulaR1C1 = "=""CP-0001;""&RC[4]&"", ""&""CP-0002;""&RC[4]&"", ""&""CP-0003;""&RC[4]"
.Value = .Value
End With

End Sub
 
Upvote 0
Thanks so much, Phuoc.
The script almost works, but the output is e.g.
CP-0001;12345, CP-0002;12345, CP-0003;12345 when I needed it to be on three separate rows e.g.

CP-0001;12345
CP-0002;12345
CP-0003;12345

Apologies, I probably didn't make this clear in my initial post. What do I need to amend to make this possible?

Many thanks
Jon
 
Upvote 0
Try this... Put CP-0001; in cell T1, CP-0002; in T2 and CP-0003; in T3 and your items in E1:E5

VBA Code:
Sub vba_concatenate()

    ActiveCell.FormulaR1C1 = _
        "=CONCAT(R1C20,RC[4],"" "",R2C20,RC[4],"" "",R3C20,RC[4])"
    Range("A1").Select
    Selection.AutoFill Destination:=Range("A1:A100"), Type:=xlFillDefault
    Range("A1:A100").Select
 
End Sub
 
Upvote 0
Try this... Put CP-0001; in cell T1, CP-0002; in T2 and CP-0003; in T3 and your items in E1:E5

VBA Code:
Sub vba_concatenate()

    ActiveCell.FormulaR1C1 = _
        "=CONCAT(R1C20,RC[4],"" "",R2C20,RC[4],"" "",R3C20,RC[4])"
    Range("A1").Select
    Selection.AutoFill Destination:=Range("A1:A100"), Type:=xlFillDefault
    Range("A1:A100").Select
 
End Sub
Cancel above... need to redo
 
Upvote 0
Apologies... not getting right... will maybe need to use a For Loop to do each row.. I am still learning on that side...
 
Upvote 0
Update code:

VBA Code:
Sub vba_concatenate_V2()
With Range("A1:A100")
.FormulaR1C1 = "=""CP-0001;""&RC[4]&CHAR(10)&""CP-0002;""&RC[4]&CHAR(10)&""CP-0003;""&RC[4]"
.Value = .Value
.WrapText = True
End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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