Adding a hide button in Excel lets you quickly hide or unhide rows, columns, or entire sheets with a single click, instead of right-clicking and choosing Hide each time. You can build one using a Form Control button and a short VBA macro, or use a simple checkbox linked to a macro. This guide walks you through the fastest method: a button that toggles hiding for a selected range or a specific sheet.
What You’ll Need
- Microsoft Excel for Windows (the steps work in Excel 2016 and later; Mac steps are noted where they differ).
- A workbook saved as a macro-enabled file (.xlsm) if you want the button to work after closing and reopening.
- Basic familiarity with the Ribbon and the Visual Basic for Applications (VBA) editor.
Step-by-Step
- Decide what the button will hide. For this guide, we’ll make a button that hides or unhides the selected rows. If you prefer columns or a specific sheet, adjust the macro accordingly (see Tips).
- Enable the Developer tab. If you don’t see it in the Ribbon, go to File > Options > Customize Ribbon, check Developer in the right pane, and click OK.
- Insert a button. On the Developer tab, click Insert and under Form Controls, choose the Button (Form Control) icon. Click and drag on the worksheet to draw the button. The Assign Macro dialog appears.
- Name the macro. In the dialog, type a name like
ToggleHideRows(no spaces) and click New. This opens the VBA editor. - Write the macro. In the editor, you’ll see a module with an empty subroutine. Replace it with the code below. This macro toggles hiding for the currently selected rows.
Sub ToggleHideRows()
Dim rng As Range
Dim isHidden As Boolean
' Use the selected range; if nothing selected, exit
If Selection Is Nothing Then Exit Sub
Set rng = Selection.EntireRow
' Check if any row in the selection is hidden
isHidden = False
Dim cell As Range
For Each cell In rng
If cell.EntireRow.Hidden = True Then
isHidden = True
Exit For
End If
Next cell
' Toggle: if any hidden, unhide all; otherwise hide all
If isHidden Then
rng.EntireRow.Hidden = False
Else
rng.EntireRow.Hidden = True
End If
End Sub
- Close the VBA editor. Click the X or press Alt+Q to return to Excel.
- Test the button. Select any rows (for example, rows 2 to 5), then click the button. The rows hide. Click again to unhide.
- Save as macro-enabled. Press Ctrl+S. If the file is not already .xlsm, Excel will prompt you to save as a macro-enabled workbook. Choose Yes and keep the .xlsm extension.
Mac difference: On Mac, the Developer tab is enabled via Excel > Preferences > Ribbon & Toolbar. The VBA editor opens with Option+Fn+F11 (or just Fn+F11). The macro code works identically.
Common Mistakes
- Forgetting to save as .xlsm: If you save as .xlsx, the macro is discarded and the button stops working.
- Selecting entire columns instead of rows: The macro above uses
Selection.EntireRow. If you select columns, it will still hide rows, which may be confusing. Adjust the macro if you want columns. - No selection: If you click the button without selecting any cells, the macro does nothing. Add a message box if you want a warning.
- Macro security settings: If macros are disabled, the button won’t run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose Disable VBA macros with notification (recommended) or Enable all macros (less secure).
- Using ActiveX instead of Form Control: ActiveX buttons require design mode and can be more finicky. Stick with Form Control for simplicity.
Tips
- Hide columns instead: Replace
.EntireRowwith.EntireColumnin the macro and rename itToggleHideColumns. - Hide a specific sheet: To toggle hiding an entire sheet (like “Sheet2”), use this macro:
Sheets("Sheet2").Visible = Not (Sheets("Sheet2").Visible = xlSheetVisible)but note that at least one sheet must remain visible. - Add a label: Right-click the button, choose Edit Text, and type a clear label like “Hide/Show Rows”.
- Use a checkbox instead: On the Developer tab, insert a Check Box (Form Control), assign the same macro, and the checkbox will toggle as well.
- Keyboard shortcut: You can also assign a shortcut key to the macro via Developer > Macros > Options, but the button is more visual.
Troubleshooting
- Button does nothing when clicked: Check that the macro is assigned: right-click the button, select Assign Macro, and confirm the correct macro is selected. Also ensure macros are enabled (see Common Mistakes).
- Error “Compile error: User-defined type not defined”: This usually means the VBA project is missing a reference. Go to Tools > References in the VBA editor and make sure Visual Basic for Applications is checked (it usually is by default).
- Rows hide but won’t unhide: The macro checks if any row in the selection is hidden. If you select only one row that is already hidden, it will unhide. If you select a mix, it unhides all. If it still won’t unhide, ensure the rows aren’t hidden by a filter (use Data > Clear to remove filters).
- Button disappears or moves: If you insert rows above the button, it moves with the cells. To keep it fixed, place it in a frozen area or use a shape instead (Insert > Shapes) and assign the macro via right-click > Assign Macro.
- Macro works but button is grayed out: This can happen if the sheet is protected. Unprotect the sheet (Review > Unprotect Sheet) or allow editing of objects in the protection settings.
Frequently Asked Questions
- Can I hide rows without VBA? Yes, you can use the Group feature (Data > Group) to add a plus/minus button that hides rows, but it’s not a custom button and only works on contiguous rows.
- Will the button work if I share the workbook? Yes, but the recipient must enable macros. Also, the file must stay .xlsm.
- Can I make the button hide rows only if a certain cell has a value? Yes, modify the macro to check a cell’s value, e.g.,
If Range("A1").Value = "" Then ... - How do I delete the button? Right-click the button and press Delete. The macro remains in the VBA project unless you remove it.
- Is there a way to undo a hide after clicking the button? Yes, press Ctrl+Z immediately after clicking, or click the button again to toggle. Note that VBA actions cannot be undone with Ctrl+Z, so the toggle is the reliable way.
- Can I use this on Excel for the web? No, Excel for the web does not support VBA macros. You can use the Group feature instead.
Related
- Flipkart Data Entry Jobs From Home: Real Facts
- How to Add Enter in Excel Cell (New Line)
- Control D in Google Sheets: What It Does
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Stop Showing Page Break Lines: FixesTo stop Excel from showing page break lines, go to File > Options > Advanced, find Display options…
- INDEX MATCH in Google Sheets: How It Works and ExamplesYes. Google Sheets supports both INDEX and MATCH, and combining them lets you find a value in one…
- Google Sheets Filter Multiple Columns: How It WorksYes. In Google Sheets in a web browser, select Data > Create a filter, then set a filter…
- How to Create a Reusable Stationery Template in WordTo create a reusable stationery template in Word, design the page, place recurring elements such as a logo…
- What Is a 5 Year Balloon? Loan & Mortgage TermA 5-year balloon is a type of loan or mortgage where you make regular monthly payments for five…
- Word Page Numbers Without the First Page: How to Set It UpTo add page numbers in Microsoft Word without showing one on the first page, insert page numbers, then…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.