How do I refer to a worksheet with a variable?

Tardisgx

Board Regular
Joined
May 10, 2018
Messages
81
Hi

Right now I open a new worksheet in my code and go back and forth by doing thisworkbook & previouswindow.

This goes wrong if I have a third workbook open and it just managed to delete a bunch of formulas the original document.

How do I open a new workbook and immediately say this workbook shall be known as "Derrick". Then switch to and from this workbook & Derrick?

I could immediately save the document in a location and refer to it physically but that seems like a lot to achieve a solid reference.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How are you opening/creating the new worksheet (or is it a workbook)?

If it is a workbook you can open/create it and set a reference to it like this.
Code:
Dim Derrick As Workbook

    Set Derrick = Workbooks.Open("C:\TheNewWorkbook.xlsx") ' Workbooks.Add

You can then use the reference you've created, i.e. Derrick, to refer to the workbook that's been opened/created.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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