Inserting Cell Text into a VBA Macro String

simonharry

New Member
Joined
Jun 17, 2016
Messages
8
Morning All,

Happy Friday!

So I am trying to create a macro that will use a pre determined string of text and then at 3 different points, insert the contents of 3 cells on the worksheet.

So far I have the following but I have hit a wall.

Sub Macro2()
'
' Macro2 Macro
'

'
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"I am part 1 of the text " &G15& " i am part two " &G16& " I am part three" &G17& " and I am the last bit."
Range("A2").Select
End Sub

As you can see, cells G15, G16 and G17 are where the text I have entered needs to be picked up from and inserted.

Any help anyone can offer would be enormously appreciated.

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi, happy Friday and welcome to the forum!

You could try one of these:

Code:
'If you want a the result to be a formula:
Range("A1").Formula = "=""I am part 1 of the text"" & G15 & "" i am part two "" & G16 & "" I am part three "" & G17 & "" and I am the last bit."""


'If you want the result to be fixed text:
Range("A1").Value = "I am part 1 of the text " & Range("G15") & " i am part two " & Range("G16") & " I am part three " & Range("G17") & " and I am the last bit."
 
Upvote 0
Code:
[COLOR=#333333]Sub DoYourselfAFavorAndDontCallItMacro2()[/COLOR]

[COLOR=#333333]Range("A1").Value = [/COLOR][COLOR=#333333]"I am part 1 of the text " & ActiveSheet.Range("G15").Value & " I am part two " & [/COLOR][COLOR=#333333]ActiveSheet.Range("G16").Value [/COLOR][COLOR=#333333]& " I am part three " & [/COLOR][COLOR=#333333]ActiveSheet.Range("G17").Value [/COLOR][COLOR=#333333]& " and I am the last bit."[/COLOR]

[COLOR=#333333]End Sub

[/COLOR]

Change ActiveSheet. to Sheets("nameofsheet"). if you are making changes to sheets that aren't in focus.




alternatively

Code:
[COLOR=#333333]Sub DoYourselfAFavorAndDontCallItMacro2()[/COLOR]

[COLOR=#333333]Range("A1").Value = [/COLOR][COLOR=#333333]"I am part 1 of the text " & [G15] & " I am part two " & [/COLOR][COLOR=#333333][G16][/COLOR][COLOR=#333333] [/COLOR][COLOR=#333333]& " I am part three " & [/COLOR][COLOR=#333333][G17][/COLOR][COLOR=#333333] [/COLOR][COLOR=#333333]& " and I am the last bit."[/COLOR]

[COLOR=#333333]End Sub

[/COLOR]
 
Upvote 0
One other question if I may please, if the string of text I am using is too long for a single line in the VBA editor in Excel, how do I split it?

I've tried & _ but that isn't working.

Thanks in advance.
 
Upvote 0
Code:
[COLOR=#333333]Range("A1").Value = [/COLOR][COLOR=#333333]"I am part 1 of the text " & ActiveSheet.Range("G15").Value _ 
& " I am part two " & [/COLOR][COLOR=#333333]ActiveSheet.Range("G16").Value [/COLOR][COLOR=#333333]& " I am part three " & [/COLOR][COLOR=#333333]ActiveSheet.Range("G17").Value [/COLOR][COLOR=#333333]& " and I am the last bit."[/COLOR]


'Just dont put the _ inside a text string and it should work
 
Last edited:
Upvote 0
if the string of text I am using is too long for a single line in the VBA editor in Excel, how do I split it?

When building long strings I like to do it like this:

Code:
Sub m()
Dim myText As String


myText = myText & "I am part 1 of the text " & Range("G15")
myText = myText & " i am part two " & Range("G16")
myText = myText & " I am part three " & Range("G17")
myText = myText & " and I am the last bit."


Range("A1").Value = myText


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,603
Messages
6,125,771
Members
449,259
Latest member
rehanahmadawan

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