How To Change the state of a custom CommandBar button using VBA in Excel?
Tags: tochangethestateofac, vbamacros
Published: 2021-10-18 04:02:05

---
title: How To Change the state of a custom CommandBar button using VBA in Excel?
published: true
date: 2021-10-14 07:25:59 UTC
tags: ToChangeTheStateOfAC,VBAMacros
canonical_url: https://geekexcel.com/how-to-change-the-state-of-a-custom-commandbar-button-using-vba-in-excel/
---

In this tutorial, you will learn how to  **change the state of a custom CommandBar button**   **using VBA ** in Microsoft Excel. 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)

## 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://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

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

```
Sub ToggleButtonState()
Dim m As CommandBarControl
    Set m = CommandBars("CommandBarName").Controls(1)
    If m.State = msoButtonDown Then
        m.State = msoButtonUp
    Else
        m.State = msoButtonDown
    End If
    Set m = Nothing
End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Save-the-Code-47.png)<figcaption>Save the Code</figcaption>

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

![Choose Macro option](https://eadn-wc04-371788.nxedge.io/cdn/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://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Run-the-Code-39.png)<figcaption>Run the Code</figcaption>

- Finally, after running the macro, you will get the  **output ** as change the state of a **custom CommandBar button** displays as **depressed or not**.

## Sum-Up

In the above short tutorial, we have illustrated ** ** how to  **change the state of a custom CommandBar button using VBA**  in Microsoft Excel. Hope you like this article. Please feel free to state your  **query**  or  **feedback**  for the above article. Thank you so much for Reading!! To learn more, check out **[Geek Excel](https://geekexcel.com/)!! **And [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How To Determine which CommandBar button started a macro using VBA?](https://geekexcel.com/how-to-determine-which-commandbar-button-started-a-macro-using-vba/)**
- **[Excel Formulas to Add Workdays to Date with Custom Weekends!!](https://geekexcel.com/excel-formulas-to-add-workdays-to-date-with-custom-weekends/)**
- **[Excel Formulas to Generate a Series of Dates by Custom Days ~ Easily!!](https://geekexcel.com/excel-formulas-to-generate-a-series-of-dates-by-custom-days/)**
- **[How to Format data with custom number formats using VBA in Excel?](https://geekexcel.com/how-to-format-data-with-custom-number-formats-using-vba-in-excel/)**