Author: Oscar Cronquist Article last updated on May 27, 2020

Click a cell to make a column hidden or visible

This example demonstrates how to build a macro that hides/shows a specific column if a given cell is selected. Each time the cell is selected the column toggles between visible/hidden.

The image above shows an example that hides/shows column E if any cell in column D is selected. This is made possible using Event code which is somewhat different than regular VBA code.

Event code is stored in a worksheet or workbook module instead of a regular code module, I will show you in detail how it works.

Event code

'Event code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'Check if any cell in column D is selected and that only one cell in column D is selected
If Not Intersect(Target, Range("D:D")) Is Nothing And Target.Cells.CountLarge = 1 Then

    'Check if column e is hidden
    If Range("E:E").EntireColumn.Hidden = True Then

        'Show column E
        Range("E:E").EntireColumn.Hidden = False

    'Continue here if column E is visible
    Else

        'Hide column E
        Range("E:E").EntireColumn.Hidden = True
    End If
End If
End Sub

Back to top

Where to put the event code?

Click a cell to make a column hidden or visible insert event code

  1. Right-click on the worksheet name, see image above.
  2. Click "View Code". Visual Basic Editor opens, see image below.
    Click to hide or show a column
  3. Paste code to the worksheet module. The arrow in the image above shows where to put the event code.
  4. Exit VB Editor and return to Excel.
Note, save the workbook with file extension *.xlsm (macro-enabled workbook) to attach the code to the workbook.

Animated image

Click to show or hide a column vba

Back to top

Click a button to hide/show a column

Click Button to hide column

The animated image above shows that you can click a button that runs a macro to hide or show a given column. This macro is saved in a regular module.

The button is a Form Control that you can easily create from the Developer tab, I will show you how later in this article.

VBA code

'Name macro
Sub HideColumn()

'Check if column F is hidden
If Range("F:F").EntireColumn.Hidden = True Then

    'Show column F
    Range("F:F").EntireColumn.Hidden = False

'Continue here if column F is visible
Else

    'Hide column F
    Range("F:F").EntireColumn.Hidden = True
End If

End Sub

 

Back to top

Where to put the macro code?

Click button to hide column VB Editor 1

  1. Copy above VBA code.
  2. Press shortcut keys Alt + F11 to open the Visual Basic Editor (VBE).
  3. Click "Insert" on the top menu, see image above.
  4. Click "Module" from the drop-down list. A module appears in the Project Explorer to the left.
  5. Paste VBA code to module window.
  6. Exit VBE and return to Excel.
Note, save the workbook with file extension *.xlsm (macro-enabled workbook) to attach the code to the workbook.

Click button to hide column Create button

  1. Click the "Developer" tab on the ribbon. Search the internet for "Show Developer tab" and your Excel version if it is missing.
  2. Click "Insert" button on the "Developer" tab.
  3. Click the button in the Form Controls group.
  4. Move the mouse cursor where you want to place the button on the worksheet.
  5. Click and hold with left mouse button.
  6. Drag with mouse to build a button. Don't worry, you can change the size and location later.
  7. Release left mouse button.
  8. A dialog box appears.
    Click button to hide column link button to a macro
  9. Click the macro name, in this case, HideColumn.
  10. Click OK button to dissmiss the dialog box.
Note. The button will be created if you click Cancel, however, no macro is linked. You can do that later if you prefer.
Right-click on the button, a pop-up menu appears. Click "Assign Macro...". The dialog box shows up again asking for a macro.

Back to top

How to hide a row with a button

Click Button to hide row

The example demonstrates a button that hides a specific row, click the button with the left mouse button and it will be hidden if the current state is visible and vice versa.

The animated image above shows a macro that hides entire row 5 when the user clicks on the button named "Hide/Show". The macro toggles row 5 between visible and hidden based on the current condition.

Back to top

VBA Code

'Name macro
Sub HideRow()

'Check if row 5 is hidden
If Range("5:5").EntireRow.Hidden = True Then

    'Show row 5
    Range("5:5").EntireRow.Hidden = False

'Continue here if row 5 is visible
Else

    'Hide row 5
    Range("5:5").EntireRow.Hidden = True
End If

End Sub

The following VBA macro hides multiple rows, in this example row 5 and 7 are hidden.

'Name macro
Sub HideMultipleRows()

'Save two strings to array variable rW
rW = Array("5:5", "7:7")

'Iterate through values in array variable rW
For Each r In rW

'Check if row is hidden based on value in variable r
If Range(r).EntireRow.Hidden = True Then

    'Show row based on variable r
    Range(r).EntireRow.Hidden = False

'Continue here if row is visible
Else

    'Hide row
    Range(r).EntireRow.Hidden = True
End If

'Continue with next value in array
Next r

End Sub

Where to put the macro code?

How to link macro to the button?

Back to top


Back to top