[VBA] Make "wbyr" a global variable

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
626
Office Version
  1. 2010
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,786
Office Version
  1. 365
Platform
  1. Windows
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.
 

CephasOz

Board Regular
Joined
Feb 18, 2020
Messages
206
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
626
Office Version
  1. 2010
Platform
  1. Windows
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

Working beautifully, thank you!
 

Watch MrExcel Video

Forum statistics

Threads
1,132,913
Messages
5,655,919
Members
418,253
Latest member
TheJackal26

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
Top