Author: Oscar Cronquist Article last updated on February 15, 2023

Select a cell to hide show column

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?

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

  1. Press with right mouse button on on the worksheet name, see image above.
  2. Press with left mouse button on "View Code". Visual Basic Editor opens, see image below.
    Select 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

Press with left mouse button on to show or hide a column vba

Back to top

Press with left mouse button on a button to hide/show a column

Press with left mouse button on Button to hide column

The animated image above shows that you can press with left mouse button on 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?

Select 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. Press with left mouse button on "Insert" on the top menu, see image above.
  4. Press with left mouse button on "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.

Select button to hide column Create button

  1. Press with left mouse button on the "Developer" tab on the ribbon. Search the internet for "Show Developer tab" and your Excel version if it is missing.
  2. Press with left mouse button on "Insert" button on the "Developer" tab.
  3. Press with left mouse button on the button in the Form Controls group.
  4. Move the mouse cursor where you want to place the button on the worksheet.
  5. Press 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.
    Press a button to hide column link button to a macro

  9. Press with left mouse button on the macro name, in this case, HideColumn.
  10. Press with left mouse button on OK button to dissmiss the dialog box.
Note. The button will be created if you press with left mouse button on Cancel, however, no macro is linked. You can do that later if you prefer.
Press with right mouse button on on the button, a pop-up menu appears. Press with left mouse button on "Assign Macro...". The dialog box shows up again asking for a macro.

Back to top

How to hide a row with a button

Select Button to hide row
Press with left mouse button on Button to hide row

The example demonstrates a button that hides a specific row, press with left mouse button on 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 press with left mouse button ons 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