VBA & Macro Problems

Excel VBA Macro to Hide and Unhide Questionnaire Rows

Olivia MillerOlivia Miller Dec 2, 2026 868 views

Question details

Create a worksheet event macro that controls questionnaire rows based on multiple drop-down selections and clears dependent answers.

Hide and Unhide Questionnaire Rows with an Excel VBA Macro
Product
Microsoft Excel
Device & OS
not provided
Scenario
An Excel questionnaire uses drop-down cells C13, C25, C37, and C49 to guide users through successive questions.
Observed behavior
Selections in C13, C25, and C37 should show or hide the relevant row groups and reset downstream selections without triggering recursive events.
Before you start

Confirm that the questionnaire and drop-down cells are on the same worksheet, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). The row ranges in the macro assume that rows 22–32, 33–44, and 45–58 contain the successive question sections.

Solution 1Recommended

Add a Worksheet_Change macro to control questionnaire rows

Use this worksheet event to update row visibility and clear dependent answers whenever a controlling drop-down changes.

The macro responds to single-cell changes in C13, C25, and C37. C49 is a downstream answer that is cleared when an earlier selection changes; it does not control another row group in this example.

The code treats blank or unrecognized selections as a reset. It disables events while clearing cells and restores them through the cleanup handler.

1
Open the worksheet code module

In Excel, right-click the questionnaire sheet tab, select View Code, and locate that worksheet's code window in the Visual Basic Editor.

2
Insert the event procedure

Paste the following code into the worksheet code window. If a Worksheet_Change procedure already exists there, merge the logic into it rather than adding a second procedure with the same name: Private Sub Worksheet_Change(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub If Intersect(Target, Me.Range("C13,C25,C37")) Is Nothing Then Exit Sub On Error GoTo CleanUp Application.EnableEvents = False Select Case Target.Address Case "$C$13" Me.Range("C25,C37,C49").ClearContents Me.Rows("33:58").Hidden = True Select Case Me.Range("C13").Value Case "Code 2", "Code 3" Me.Rows("22:32").Hidden = False Case Else Me.Rows("22:32").Hidden = True End Select Case "$C$25" Me.Range("C37,C49").ClearContents Me.Rows("45:58").Hidden = True Select Case Me.Range("C25").Value Case "Yes" Me.Rows("33:44").Hidden = False Case Else Me.Rows("33:44").Hidden = True End Select Case "$C$37" Me.Range("C49").ClearContents Select Case Me.Range("C37").Value Case "Yes" Me.Rows("45:58").Hidden = False Case Else Me.Rows("45:58").Hidden = True End Select End Select CleanUp: Application.EnableEvents = True End Sub

3
Save and test the drop-downs

Save the workbook as .xlsm, return to the questionnaire sheet, and select Code 2 or Code 3 in C13. Test Yes and No in C25 and C37, then change C13 to Code 1 or Code 4 and confirm that rows 22–58 are hidden and downstream answers are cleared.

Add a Worksheet_Change macro to control questionnaire rows
Single-cell changes only: The procedure ignores changes to multiple cells at once. It also does not apply visibility rules automatically when the workbook opens; test or set the initial row visibility separately.
Free Microsoft Office alternative

Edit questionnaire spreadsheets with WPS Office

WPS Office is a free, lightweight alternative for everyday spreadsheet work, with a familiar interface and Microsoft Office format compatibility that makes moving existing files easier. Use Excel to implement and test this Excel-specific VBA event; WPS Office does not fix Excel macro issues.

Work with questionnaire spreadsheets in WPS Spreadsheets.Open and edit Microsoft Excel formats such as .xlsx.Use a familiar spreadsheet interface for easier migration.Access a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Where should I paste the Worksheet_Change code?

Right-click the questionnaire sheet tab in Excel, select View Code, and paste the procedure into that worksheet's code window, not a standard module.

Why does the macro disable Application.EnableEvents?

Clearing C25, C37, or C49 can trigger another change event. Setting Application.EnableEvents to False prevents recursive calls, and the CleanUp handler restores it to True.

What happens when I select Code 1 or Code 4 in C13?

The macro hides rows 22–58 and clears C25, C37, and C49. Selecting Code 2 or Code 3 instead shows rows 22–32 and keeps the later sections hidden until their controlling answers are chosen.

Why does changing C49 not show or hide rows?

C49 is treated as a downstream answer, not a controlling drop-down. The example clears it when C13, C25, or C37 changes, but it has no row-visibility rule of its own.

Will the macro run after I paste values into several cells at once?

No. The Target.CountLarge check exits when more than one cell changes. Change the controlling drop-downs individually, or revise the procedure if bulk edits must be supported.