Assigning Open Workbook to variable

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
If I already have a workbook open, and know the name of the workbook, why can't I simply assign the workbook to a variable?

Example:

If I have workbook A and workbook B, and both are open.
The code is in workbook A.

Why can't I do something simple like:

Code:
Sub Assign_WB

Dim wb as Workbook

Set wb = Workbooks("Workbook B")

For Each ws In wb.Worksheets

etc etc etc

Next

End Sub

It seems like all the ways I have been doing it (as per research and internet) involve more code than what I would assume is needed to simply state:

Variable X is "Workbook B"

Granted the code I have been using works, but I just don't fully understand why ... and why the above snippet wouldn't work ....

I am looking for more of the concept/principle than fixing the above code .... but writing the above code correctly would help with understanding the concept/principle.

-Spydey
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If a workbook is open, you need only its name for identification, but that name must include the file extension. So,

Dim wb as workbook
Set wb = Workbooks("Workbook B.xlsm")

is ok, but
Set wb = Workbooks("Workbook B")
will not fly.
 
Upvote 0
You can actually use
Code:
Set wb = Workbooks("Workbook B")
if your folder settings are set to "Hide known file extensions".

That said I would recommend always including the extension
 
Upvote 0
@ JoeMo & Fluff

Thanks for the insight, I appreciate it.

So if I include the file extension type in the code, then it should work fine, correct?

Hmmm, ok.

But it must be the file name, not the whole path, right?

So

Code:
Dim wb as Workbook

Set wb = Workbooks("WorkBook B.xlsm")

would work, as you both pointed out ....

but what about:

Code:
Dim wb as Workbook

Set wb = Workbooks("C:\Location\Of\File\Workbook B.xlsm"

Granted, workbook B is already open.

I tend to get errors with that ..... and I am trying to figure out the different requirements for the use of the workbooks object (it is an object, right???)


-Spydey

P.S. This is just me fiddling around, trying to better understand VBA, objects, items, etc etc., and their correct uses.
 
Upvote 0
If the workbook is already open, using the full name (path + name) will trigger an error.

Here's a simple UDF I use to avoid errors altogether. If the function returns False then you can open the workbook using the full name.
Code:
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
   
WorkBookNotOpen:
End Function
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,659
Members
449,178
Latest member
Emilou

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