EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Coding VBA Global Variables
Secondary Sidebar
VBA Coding
  • VBA Coding
    • VBA Set Range
    • VBA ByVal
    • VBA Long
    • VBA Data Types
    • VBA Enum
    • VBA Clear Contents
    • VBA Call Sub
    • VBA Exit Sub
    • VBA END
    • VBA Class Module
    • VBA excel programming
    • VBA Code
    • VBA Option Explicit
    • VBA Global Variables
    • VBA Variable Declaration
    • VBA SUB
    • VBA ByRef
    • VBA Variant
    • VBA Public Variable
    • VBA Variable Types
    • VBA Goal Seek
    • VBA Variable Range
    • VBA Class Module
    • VBA XLUP
    • VBA XML
    • VBA ByVal

VBA Global Variables

By Ashwani JaiswalAshwani Jaiswal

VBA Global Variables

Excel VBA Global Variables

We usually declare any variable by using the word DIM and this is the best direct method of doing this. But the variables declared using DIM can only be used within the same module. If we try to use this variable defined by using DIM, it will be applicable for the module under which it is being declared. What if we want to use a variable in any module or project? Which cannot be possible if we keep on using DIM to define that variable?

In that case, we have a secondary option by defining the variable by word Global or Public in VBA. VBA Global or Public variables are those variables which are declared at the beginning of subcategory with the help of Option Explicit. Whatever things which we don’t or can’t do under subcategories, they are done before that under Option Explicit.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Declare Global Variables in VBA?

Below are the different examples to declare a global variable in Excel using VBA code.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (85,982 ratings)
You can download this VBA Global Variables Excel Template here – VBA Global Variables Excel Template

VBA Global Variables – Example #1

In this example, we will see how different subcategories can be used as a single module without using Global or Public word for defining any variable. For this, we would need a module.

  • Go to Insert menu and click on Module as shown below.

VBA Global Variables Example 1-1

  • Now type the subcategory in any name as shown below.

Code:

Sub Global_Variable()

End Sub

VBA Global Variables Example 1-2

  • Now use DIM to define any kind of variable. Here we have chosen alphabet A as String.

Code:

Sub Global_Variable()

  Dim A As String

End Sub

VBA Global Variables Example 1-3

  • Now create another subcategory in the same module as shown below.

Code:

Sub Global_Variable()

  Dim A As String

End Sub

Sub Global_Variable6()

End Sub

VBA Global Variables Example 1-4

  • Now in the second subcategory define another variable B using DIM as String.

Code:

Sub Global_Variable()

  Dim A As String

End Sub

Sub Global_Variable6()

  Dim B As String

End Sub

Declare Variable B Example 1-5

As shown above, both the variables A and B defined in both subcategories cannot be used in each other’s region. Those will be applicable in their own subcategory only.

  • Now to make this work, write Option Explicit above the first subcategory as shown below.

Code:

Option Explicit

Sub Global_Variable()

End Sub

Sub Global_Variable6()

End Sub

VBA Global Variables Example 1-6

  • Now we can define our variable which will be used in both of the subcategories of which we have below. So now consider writing any variable say A as String using DIM.

Code:

Option Explicit

  Dim A As String

Sub Global_Variable()

End Sub

Sub Global_Variable6()

End Sub

VBA Global Variables Example 1-7

As we have defined the variable all the sub-categories will be in that module and this would be applicable to all the code of that module only. And if we try to call the variables defined in this module to some other module, then it would give us the error.

VBA Global Variables – Example #2

In this example, we will see how to use Global or Public word for defining any variable in excel VBA. For this, we will use the code which we have seen in example-1.

We will use below portion of code for Global or Public variable declaration.

Code:

Option Explicit

Sub Global_Variable()

End Sub

Sub Global_Variable6()

End Sub

VBA Global Variables Example 2-1

As we did in example-1 where we have declared the common variable which would be used in both the subcategories, below the Option Explicit. In the example, we will declare the Global Variable below the Option Explicit as well.

As we already have our code ready so we can directly go on declaring variables using Global. Now in the below Option Explicit write Global the same we used to with DIM and select a variable of choice. Here we are choosing alphabet A as shown below.

Code:

Option Explicit

Global A

Sub Global_Variable()

  Dim A As String

End Sub

Sub Global_Variable6()

  Dim B As String

End Sub

VBA Global Variables Example 2-2

Now we choose any type of variable to be it. As we have already used String in the above example, so we would use the same here as well.

Code:

Option Explicit

Global A As String

Sub Global_Variable()

  Dim A As String

End Sub

Sub Global_Variable6()

  Dim B As String

End Sub

VBA Global Variables Example 2-3

This completes our Global variable declaration process. Now we can use this in any module, any project as String only. Before we use it, delete the previously declared variable, then the code would look like as shown below.

Code:

Option Explicit

Global A As String

Sub Global_Variable()

End Sub

Sub Global_Variable6()

End Sub

VBA Global Variables Example 2-4

Now let’s assign some text to defined variable A in both subcategories. We are choosing “Test1” and “Test2” for variable A in both subcategories as shown below. And also we have chosen MsgBox to show the values stored in variable A.

Code:

Option Explicit

Global A As String

Sub Global_Variable()

A = "Test1"
MsgBox A

End Sub

Sub Global_Variable6()

A = "Test2"
MsgBox A

End Sub

MsgBox Example 2-5

Now run the code by pressing the F5 key or by clicking on the play button to see the result.

Test1 Example 2-6

We will get the message as “Test1” as shown above. It is because we had kept our cursor in the first subcategory.

Now put the cursor anywhere in the second subcategory and run the code again. We will now get the message box with message “Test2” as shown below.

Test2 Example 2-7

This is how we can create and define a variable once with the help of Global and that can be used in any module, any class and in any project. We can use Public as well in place of Global as shown below. And this will give the same result as we got in using Global.

Code:

Option Explicit

Public A As String

Sub Global_Variable()

A = "Test1"
MsgBox A

End Sub

Sub Global_Variable6()

A = "Test2"
MsgBox A

End Sub

Use Public Example 2-9

Pros of VBA Global Variable

  • It saves time in declaring the multiple variables when we know, we may need to define the same type of variable again in different modules or subcategories.
  • By using Global or Public in defining a variable process, our code becomes smaller saving time and spaces.
  • It reduces the complexity when we are writing the huge code and may get confused among the use of different variables in different modules or subcategories.

Cons of VBA Global Variable

  • If we do any changes to the Global variable then that changes will get implemented in all where it is used causing the problem in the functionality of written code.

Things to Remember

  • Global and Public can be used interchangeably with the same functionality.
  • If we don’t want to use Global variable then we can follow the process as shown in example-1 for defining the variable. But that would be limited to the same module.
  • Global variables can be used in any module, any subcategory, any class or in any project.
  • A Global variable gives the output of the code where we have kept the cursor. It will not run the complete code at one go giving all the output one by one.

Recommended Articles

This is a guide to VBA Global Variables. Here we discuss how to declare Global Variables in Excel using VBA code along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA UserForm
  2. VBA Get Cell Value
  3. VBA Activate Sheet
  4. VBA RGB
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Excel VBA Bundle (120+ Courses, 500+ hours of Videos) Learn More