How to use Excel VBA Variable Scope in Excel Office 365?
Tags: touseexcelvbavariabl, vbamacros
Published: 2021-09-06 01:20:42

---
title: How to use Excel VBA Variable Scope in Excel Office 365?
published: true
date: 2021-09-05 17:47:20 UTC
tags: ToUseExcelVBAVariabl,VBAMacros
canonical_url: https://geekexcel.com/how-to-use-excel-vba-variable-scope-in-excel-office-365/
---

In this article, you will learn **how to use Excel VBA Variable Scope in Excel Office 365**. Let’s see them below!! Get the official version of **MS Excel** from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## VBA Variable Scope

In all the **programming languages** , we have **variable access specifiers** that define from where a **defined variable can be accessed**. Excel VBA is no Exception. VBA too has scope specifiers. These scope specifiers can be used to set the visibility/scope of a variable in Excel VBA.

## Types of scope specifiers

- Procedure Level
- Private – Module Level
- Public – Project Level

## VBA Procedure Level Variable Scope

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-8-1024x180.png)<figcaption>Select Visual Basics</figcaption>

- Now, you have to **copy and paste the code** given below.

```
 Option Explicit

Sub scopeTest()
 Dim x, y As Integer ' Procedure level variable in VBA
 x = 2
 y = 3
 Debug.Print x + y
End Sub

Sub sum()
x = 5
y = 7
Debug.Print x + y
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-code-9.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the **Excel Spreadsheet** , and click on the **Developer Tab**.
- You need to choose the **Macros option** in the Code section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-8-1024x184.png)<figcaption>Macro option</figcaption>

- Now, you have to make sure that your **macro name is selected** and click the **Run** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-code-3.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive the **output** in the Microsoft Excel.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output.png)<figcaption>Output</figcaption>

## VBA Private Variable- Module Level Scope

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-8-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to **copy and paste the code** given below.

```
 Option Explicit
'Module level variable in VBA. Both variables x and y are private to this module. and
'can be accessed from any sub or function within this module.
Dim x As Integer 
Private y As Integer

Sub scopeTest() 'This can be accessed from any module in the project
 x = 2
 y = 3
 Debug.Print x + y
End Sub

Private Sub sum() ' This can't be accessed from other modules
x = 5
y = 7
Debug.Print x + y
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-10-1024x499.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the **Excel Spreadsheet** , and click on the **Developer Tab**.
- You need to choose the **Macros option** in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-8-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your **macro name is selected** and click the **Run** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-4.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive the **output** in the Microsoft Excel.

## Public Variables- Project Level Scope

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-8-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to **copy and paste the code** given below.

```
 Option Explicit
'Project level variable in VBA.
Public x As Integer
Public y As Integer

Public Sub scopeTest() 'This can be accessed from any module in the project
 x = 2
 y = 3
 End Sub

Private Sub sum() ' This can't be accessed from other modules
 x = 5
 y = 7
Debug.Print x + y
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-11-1024x546.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the **Excel Spreadsheet** , and click on the **Developer Tab**.
- You need to choose the **Macros option** in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-8-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your **macro name is selected** and click the **Run** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-5.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive the **output** in the Microsoft Excel.

## Wind-Up

We hope that this short tutorial gives you guidelines to **use Excel VBA Variable Scope in Excel Office 365. ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[Excel Formulas to Remove the Text By Variable Position ~ Quickly!!](https://geekexcel.com/excel-formulas-to-remove-the-text-by-variable-position/)**
- **[Excel Formulas to Get the Maximum Value with Variable Columns!!](https://geekexcel.com/excel-formulas-maximum-value-with-variable-columns/)**
- **[Formulas to Count Variable Range with COUNTIFS Function!!](https://geekexcel.com/excel-formulas-to-count-variable-range-with-countifs-function/)**
- **[Excel Office 365 VBA Variables & Data Types - A Complete Guide!!](https://geekexcel.com/excel-office-365-vba-variables-data-types-a-complete-guide/)**
- **[How to use the CONFIDENCE.NORM function in Excel office 365?](https://geekexcel.com/how-to-use-the-confidence-norm-function-in-excel-office-365/)**