Use variable value as part of a text formula in VBA

spacecaptainsuperguy

Board Regular
Joined
Dec 30, 2004
Messages
202
Office Version
  1. 365
Platform
  1. Windows
I'm working on setting up a report to be generated by a macro and in one particular cell I want to incorporate both text and numbers in a cell. Earlier in the macro, I've defined CLoan1 as Long and assigned it a value. The line of code I'm trying to get to work is the following:

ActiveCell.FormulaR1C1 = "=""Total available for borrowing or limit of "" & Text(CLoan1, ""$#,###,000"")"

But I can't figure out how to word it so that the Text formula uses the value of CLoan1 to not produce an #NAME? error.

Any suggestions on how I can do this or a better line of code to get the end result?

Thanks in advance
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Instead of just assigning the formula you are trying to create directly to the cell like this
VBA Code:
ActiveCell.FormulaR1C1 = "=""Total available for borrowing or limit of "" & Text(CLoan1, ""$#,###,000"")"

Make life easier on yourself by declaring a string variable to hold the formula and then assign that to the cell. With the variable you can more easily use the debugger to tweak the formula until you get it right (corrected formula below).
VBA Code:
Dim FormulaStr As String
FormulaStr = "=""Total available for borrowing or limit of "" & Text(" & Cloan1 & ", ""$#,###,000"")"
Debug.Print FormulaStr

ActiveCell.FormulaR1C1 = FormulaStr
 
Upvote 0
Try this:

ActiveCell.FormulaR1C1 = "=""Total available for borrowing or limit of ""& Text(" & CLoan1 & ",""$#,###,000"")"
 
Upvote 0
Solution
Thank you both. That got my formula sorted out.

@rlv01 I have never, nor would have ever, thought to try turning it into a string variable. I've also never used the Debug.Print before so I'll have to look at that some more to see how that can help me in the future.

Very much appreciate the help!!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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