VBA: Having the Sheet name in a range reference as a variable

Mrnutle

New Member
Joined
May 3, 2011
Messages
3
I am writing a macro to populate a table. Each line in the table will show the worksheet name in column A, and reference cell N5 of that particular worksheet in column B.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Column A is the easy part. IF I record a macro of what should go in coulm "B", i get something like this<o:p></o:p>
<o:p></o:p>
ActiveCell.FormulaR1C1 = "=Building100!R[1]C[12]"<o:p></o:p>
Where "Building100" is my sheet name<o:p></o:p>
What I need is the sheet name par tof the reference to look at the cell in column "A" (Which says "Building100")<o:p></o:p>
<o:p></o:p>
I tried this<o:p></o:p>
<o:p></o:p>
Dim BldgSheet as String<o:p></o:p>
BldgSheet = Range("A4")<o:p></o:p>
Range("B4").Select<o:p></o:p>
ActiveCell.FormulaR1C1 = "=BldgSheet!R[1]C[12]"<o:p></o:p>
<o:p></o:p>
I know this sytax is wrong, because this just causes the algoryth to look for a sheet named "BldgSheet". I have actually tried a bunch of other ways, I just don't know enough to make this work.<o:p></o:p>
<o:p> </o:p>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Awsome, that works.

Do you mind going into detail about the formatting and why this works.

Do the extra quotes and ampersand designate BldgSheet as a variable?
 
Upvote 0
As BldgSheet is a variable you have to take it out of the quotes or Excel will assume it is part of the string, not a variable.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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