What is the difference Between ByRef and ByVal Arguments?
Tags: differencebetweenbyr, vbamacros
Published: 2021-09-14 02:32:59

---
title: What is the difference Between ByRef and ByVal Arguments?
published: true
date: 2021-09-13 10:15:03 UTC
tags: DifferenceBetweenByR,VBAMacros
canonical_url: https://geekexcel.com/what-is-the-difference-between-byref-and-byval-arguments/
---

Have you ever tried to learn about ByRef And ByVal Arguments in your Excel Spreadsheet? In this tutorial, we will guide you about  **what is the difference between ByRef And ByVal Arguments in VBA Excel Office 365**. Let’s get them below!! Get an 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)**

## ByRef argument

- When an **argument** is passed as a **ByRef argument** to a different **sub or function** , the **reference of the actual variable** is sent.
- If any **changes** made in to the **copy of variable** , will **reflect in original argument**.
- Then, **instead of value** , the **location of value is sent to function** using **ByRef** to a function.
- Now, this is the **default argument** in VBA and don’t need to write **ByRef before argument**.

## ByRef argument Syntax

```
Sub x(a as Variant)
Or
Sub x(ByRef a as Variant)

```

## ByRef Example

- 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-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

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

```
Sub X(ByRef a As Variant)
a = 20
Debug.Print "in sub X value of a = " & a
End Sub

Sub Y()
a = 10
Call X(a)
Debug.Print "in sub Y value of a = " & a
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-30.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-11-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-15.png)<figcaption>Run the Code</figcaption>

- Finally, you will get  **output ** as given below in the **VBA**.

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

## ByVal argument

- When an **argument** is passed as a **ByVal argument** to a **different sub or function** , only the value of the **argument is sent**.
- If any **changes** made in **foreign function or sub** will **not reflect** in the **original argument**.
- Now, to **declare an argument as ByVal** you need to use the **ByVal keyword before the argument**.

## ByVal argument Syntax

```
 Sub x(ByVal a as Variant) 
```

## ByVal Example

- 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-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

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

```
Sub X(ByVal a As Variant)
a = 20
Debug.Print "in sub X value of a = " & a
End Sub

Sub Y()
a = 10
Call X(a)
Debug.Print "in sub Y value of a = " & a
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/ByVal-Save-the-Code.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-11-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-15.png)<figcaption>Run the Code</figcaption>

- Finally, you will get  **output ** as given below in the **VBA**.

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

- Now, you have to calculate the **value of original is NOT** changed by **sub X**.
- 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-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

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

```
Sub X(ByVal a As Variant)
Debug.Print "in sub X value of a Before = " & a
a = 20
Debug.Print "in sub X value of a After= " & a
End Sub

Sub Y()
a = 10
Call X(a)
Debug.Print "in sub Y value of a = " & a
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/After-Before-Save-the-Code.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-11-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/Before-After-Run-the-Code.png)<figcaption>Run the Code</figcaption>

- Finally, you will get  **output ** as given below in the **VBA**.

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

## End of Era

We hope that this tutorial gives you guidelines on **what is the difference between ByRef And ByVal Arguments in VBA 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:**

- **[How to use the XNPV function in Excel office 365?](https://geekexcel.com/how-to-use-the-xnpv-function-in-excel-office-365/)**
- **[Excel Formulas to Highlight the Cells that End with Given Value!!](https://geekexcel.com/excel-formulas-to-highlight-cells-end-with/)**
- **[Shortcut for “Select to End of Cell” in the Worksheet!!](https://geekexcel.com/quick-excel-shortcut-for-select-to-end-of-cell-in-a-worksheet/)**
- **[Excel Shortcuts: Delete to the End of Line in the Spreadsheet!!](https://geekexcel.com/shortcuts-for-delete-to-the-end-of-line-in-ms-excel-365/)**
- **[Formulas for Counting Cells that End with certain character or text!!](https://geekexcel.com/excel-formulas-for-counting-cells-that-end-with-certain-character-or-text/)**