Concatenate string in VBA not working with R1C1 reference

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Think it's too early for me and I'm missing a trick.

I want to simply take a booking reference string and concatenate it with ' and ',

So S123456 becomes 'S123456',


Can anyone tell me why the below just results in
Excel Formula:
'',

Thanks.

VBA Code:
Range(Cells(2, Lastcol + 1), Cells(Lastrow, Lastcol + 1)).FormulaR1C1 = "''" & RC15 & "',"
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Because you haven't built a real formula string, you've used RC15 as a VBA variable (if you had Option Explicit set, you'd have got an error). You'd need:

VBA Code:
Range(Cells(2, Lastcol + 1), Cells(Lastrow, Lastcol + 1)).FormulaR1C1 = "=""'""&RC15&""',"""
 
Upvote 0
Solution
Ah I see, yes that works. I do have to add a double apostrophe to get it to write the value down as 'S123456', as opposed to S123456', - it's for users to copy and slam into SQL.

Thanks for the help once more. I see so I used Formular1c1 but it wasn't really a formula, it was built the same way you'd compile a string like cells(1,6).value = ""''" & cells(activecell.row, "Z").value & ""',""

:)
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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