I would like to define a global variable in VBA in one module and use this in other VBA modules.
I am trying to follow: How do I declare a global variable in VBA?
I have created a new module called "GlobalVariables", and first declared the Public variables and then set their value within a function (trying to do this in open code causes an error). My code is below.
But the Global variable StartYear does not seem to be available into other VBA modules. What am I doing wrong?
Option Explicit Public StartYear As Integer Public BaseYear As Integer Function DeclareGlobalVariables() StartYear = ActiveWorkbook.Worksheets("RunModel").Range("StartYear").Value BaseYear = ActiveWorkbook.Worksheets("RunModel").Range("BaseYear").Value End Function
Integervariables. This datatype may not be able to hold numeric values you are using causing an overflow error.