VBA & Macro Problems

Hide Columns on Multiple Excel Worksheets Based on a Cell Value

Ayan MasoodAyan Masood Nov 20, 2026 869 views

Question details

Use a cell value to control which columns are hidden on multiple named Excel worksheets.

Hide Columns on Multiple Excel Worksheets Based on a Cell Value
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user selects a value on the Approval sheet and maintains a list of target worksheet names in column B of the Names sheet.
Observed behavior
The desired result is for the listed worksheets to hide or show specific columns when the control cell changes.
Before you start

Create worksheets named Approval and Names, and list the target worksheet names in Names!B2 downward. Choose Approval!D3 as the control cell and save the workbook as an Excel Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use a Worksheet Change event to update listed worksheets

Run the macro automatically when Approval!D3 changes, while ignoring changes elsewhere.

This example treats 1 as hiding columns C and J through L and showing G. It treats 2 as showing C and J through L and hiding G. Change the True and False values if you need a different arrangement.

Only worksheets listed in Names!B2 downward are processed. Blank entries and names that do not match an existing worksheet are skipped.

1
Open the sheet module

In desktop Excel, open the VBA editor from Developer > Visual Basic. In the Project pane, double-click the Approval worksheet under Microsoft Excel Objects.

2
Add the event procedure

Paste the following code into the Approval sheet module: Private Sub Worksheet_Change(ByVal Target As Range) Dim namesSheet As Worksheet Dim targetSheet As Worksheet Dim nameCell As Range Dim lastRow As Long Dim choice As String If Intersect(Target, Me.Range("D3")) Is Nothing Then Exit Sub If IsError(Me.Range("D3").Value) Then Exit Sub choice = Trim$(CStr(Me.Range("D3").Value)) If choice <> "1" And choice <> "2" Then Exit Sub Set namesSheet = ThisWorkbook.Worksheets("Names") lastRow = namesSheet.Cells(namesSheet.Rows.Count, "B").End(xlUp).Row If lastRow < 2 Then Exit Sub For Each nameCell In namesSheet.Range("B2:B" & lastRow) If Not IsError(nameCell.Value) Then If Len(Trim$(CStr(nameCell.Value))) > 0 Then Set targetSheet = Nothing On Error Resume Next Set targetSheet = ThisWorkbook.Worksheets(Trim$(CStr(nameCell.Value))) On Error GoTo 0 If Not targetSheet Is Nothing Then Select Case choice Case "1" targetSheet.Range("C:C,J:L").EntireColumn.Hidden = True targetSheet.Columns("G").Hidden = False Case "2" targetSheet.Range("C:C,J:L").EntireColumn.Hidden = False targetSheet.Columns("G").Hidden = True End Select End If End If End If Next nameCell End Sub

3
Test the control cell

Return to the Approval sheet and enter or select 1 or 2 in D3. Check the worksheets listed on Names to confirm that the expected columns are hidden or shown.

Use a Worksheet Change event to update listed worksheets
Other values do not change the columns: A blank or a value other than 1 or 2 leaves the current column visibility unchanged. The event runs when D3 is edited, not when a formula in D3 recalculates.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for everyday spreadsheet work. Its familiar interface and support for Excel formats make it easy to move existing files into WPS Spreadsheets. It does not fix the Excel VBA event procedure described above.

Hide or show worksheet columns using spreadsheet controlsOpen and edit common Excel formats, including .xlsx and .xlsUse a familiar spreadsheet interface for an easier transitionAccess free, lightweight tools for everyday office work
microsoft office alternative - wps office

Frequently Asked Questions

Where should I put the Worksheet_Change code?

Open Developer > Visual Basic, then double-click the Approval worksheet in the Project pane and paste the procedure into that sheet's module. It will not run as a worksheet event if placed in a standard module.

What happens if a worksheet name is misspelled?

The procedure skips names that do not match an existing worksheet. Check the entries in Names!B2 downward against the worksheet tabs.

Will the macro run when a formula changes D3?

No. Worksheet_Change responds to edits to D3, not formula recalculation. A formula-driven control requires a different approach, such as a Worksheet_Calculate event.

Why doesn't the code run when I change D3?

Check that the procedure is in the Approval sheet module, the file is saved as .xlsm, and macros are enabled for the workbook. Also confirm that you are editing Approval!D3 and entering 1 or 2.