VBA - Reference variable in formula

jpar1983

New Member
Joined
Aug 11, 2012
Messages
25
I'm trying to reference a variable that I have declared in a formula using VBA. I have a variable that I've declared to capture the name of a specific worksheet in Excel. I'm trying to reference that variable in a cell formula, but I don't think I've got the syntax right somehow. I've looked at a lot of forums and other websites, and I thought I'd matched the syntax correctly, but the code is still erroring out. Can someone tell me what I'm doing wrong?

Dim OrigSheet As String
OrigSheet = ActiveSheet.Name
Range("A1").Select
ActiveCell.Formula = "=Row()" & OrigSheet
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This does not look like it would be creating a valid Excel formula:
VBA Code:
ActiveCell.Formula = "=Row()" & OrigSheet

Can you post an example of what you want the finished formula in cell A1 to look like?
 
Upvote 0
This does not look like it would be creating a valid Excel formula:
VBA Code:
ActiveCell.Formula = "=Row()" & OrigSheet

Can you post an example of what you want the finished formula in cell A1 to look like?
Thanks for the quick reply! I want the final result to be the row number and the sheet name, so cell A1 would be "1 Sheet1". I'm creating a reference number of sorts for a really long report that takes multiple sheets.
 
Upvote 0
Note that you don't need VBA to create the formula. You can capture the sheet name, as described here: Excel formula: Get sheet name only | Exceljet

So then you could just use the formula:
Excel Formula:
=ROW() & " " & MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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