[VBA] Make "wbyr" a global variable

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hi all, can't seem to get the last piece of the puzzle...

I have a series of subs within a module where they all reference the "workbook year", which I want to set as 2020 just once in the code. I've declared it as such:

VBA Code:
Public wbyr as Integer
wbyr = 2020

However when I run, I get:

"Invalid outside procedure"

All the guides I can see online show you how to set up a global variable, but not how to define what that variable is.

Thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I think the problem's occurring because you're not actually running any code to declare the variable.
Your line:
VBA Code:
wbyr = 2020
...isn't actually being run.

Normally, to declare a global variable, you'd declare it in the workbook_open event - or at least soon after, in order to get it into memory, ready to be used.

If you go into your "Immediate" window now, and run that line, that's set the variable, and the rest of your code should run properly.
 
Upvote 0
The declaration of the variable is fine where it is, but the assignment of the value is not. Instead, use:

VBA Code:
Public Const wbyr As Integer = 2020
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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