Declaring workbook variables and setting variables to "not" other workbooks VBA

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

Code:
Dim wb1 As WorkbookDim wb2 As Workbook
Dim wb3 As Workbook
Dim wb4 As Workbook


Set wb1 = General_Account_Ledger.xls
Set wb2 = PERSONAL.xls
Set wb3 = Products.xls
Set wb4 = wb1 Or wb2 Or wb3

I would like to write a subroutine that operates while four different workbooks are open. The fourth workbook will change regularly but will need to work with other open workbooks. I have set different variables for the four workbook, but am stumped as to how to set the fourth. Ideally, if it was possible I would set wb4 to something like

Code:
set wb4 <> wb1 Or wb2 or wb3

but its not possible to write it like this. I suspect that defining a random name sheet with a single variable will be done using "not-equal to"

Any suggestions with the logic would be greatly appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
If the 4th workbook is the active workbook when the subroutine is initiated then:

set wb4 = Activeworkbook
 
Upvote 0
I should be able to run the sub with the wb4 open so that it is the activeworkbook at the time the sub is run. let me try this...
 
Upvote 0
OK, it seems to be working, but I have another problem with the code on a different subject. the beginning of the script looks like this now

Code:
Dim wb1 As WorkbookDim wb4 As Workbook


Set wb1 = Application.Workbooks("General_Account_Ledger.xls")
Set wb4 = ActiveWorkbook

the activeworkbook will need to be open when the variable is set
 
Upvote 0
OK, it seems to be working, but I have another problem with the code on a different subject. the beginning of the script looks like this now

Code:
Dim wb1 As WorkbookDim wb4 As Workbook


Set wb1 = Application.Workbooks("General_Account_Ledger.xls")
Set wb4 = ActiveWorkbook

the activeworkbook will need to be open when the variable is set

A work book cannot be the active workbook unless it is open.
 
Upvote 0
the workbook, wb4, is on screen when I run the macro which should be OK as long as I remember to keep it as the on screen/open book
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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