How To Hide columns using a button in Excel VBA?
Tags: tohidecolumnsusingab, vbamacros
Published: 2021-09-28 11:39:47

---
title: How To Hide columns using a button in Excel VBA?
published: true
date: 2021-09-24 08:18:01 UTC
tags: ToHideColumnsUsingAB,VBAMacros
canonical_url: https://geekexcel.com/how-to-hide-columns-using-a-button-in-excel-vba/
---

In this article, we are going to see how to  **hide columns using a button ** in Excel VBA. Let’s get into this article!! 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)**

## Example

- Firstly, you need to  **create a sample data** in Microsoft Excel.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-39.png)<figcaption>Create a Sample data</figcaption>

- 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 code**  given below.

```
Sub Hide_C()
Dim C_ell As Range
ActiveSheet.Shapes.Range(Array("Button 2")).Select
If Selection.Characters.Text = "Unhide Columns" Then
Columns.Hidden = False
Selection.Characters.Text = "Hide Columns"
Else
For Each C_ell In Range("B1", Cells(1, Columns.Count).End(xlToLeft))
If C_ell = "x" Then C_ell.Columns.Hidden = True
Next
Selection.Characters.Text = "Unhide Columns"
End If
Range("A2").Select
End Sub

```

- 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-77.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>

- Then, 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-47.png)<figcaption>Run the Code</figcaption>

- Finally, you need to click on  **hide button to see the hidden column**.

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

## Wind-Up

We hope that this short tutorial gives you guidelines on how to  **hide columns using a button ** in Excel VBA. ** ** 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/) **!!**

**Sum-Up:**

- **[Count the Times a Word Appears in a Cell or a Range in Excel Office 365!!](https://geekexcel.com/count-the-times-a-word-appears-in-a-cell-or-a-range-in-excel-office-365/)**
- **[Excel Formulas to Count specific word in a range ~ Easy Tutorial!!](https://geekexcel.com/excel-formulas-to-count-specific-words-in-a-range/)**
- **[Excel Formulas to Get the Last Word of a Text String ~ Quick Method!!](https://geekexcel.com/excel-formulas-to-get-the-last-word-of-the-text-string/)**
- **[Remove the Last Word ~ Easy Tutorial!!](https://geekexcel.com/excel-formulas-to-remove-the-last-word-easy-tutorial/)**
- **[Excel Formulas to Extract the Word that Contains Specific Text ~ Easily!!](https://geekexcel.com/excel-formulas-to-extract-the-word-that-contains-specific-text/)**