Hide and Unhide Excel Rows Based on a Drop-Down Selection
Question details
Show or hide form rows automatically when a user changes a data validation selection on a protected worksheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A form uses a drop-down in C5 to choose a pay type and may use a second selection in C7 to control additional rows.
- Observed behavior
- The worksheet should display the relevant rows for each selection and restore sheet protection after the change.
Open the workbook in desktop Excel and check the exact drop-down text, cell references, row ranges, and sheet protection password. If the sheet is protected, make sure C5 and C7 are unlocked so users can change their selections.
Add a Worksheet Change Macro to the Form Sheet
Use this approach to update the form automatically when C5 or C7 changes, including when the sheet is protected.
The example shows row 7 for Annual Salary and row 16 for Hourly Wage. For Annual Salary, C7 values Option 1 and Option 2 show rows 8–9 and 10–12 respectively. Replace these example labels and ranges with the values used in your form.
The macro temporarily unprotects the sheet only if it was protected. Reapplying protection with default settings may change existing protection permissions, so adjust the Protect statement if your form needs specific options.
Right-click the form's worksheet tab in Excel and select View Code. Place the event procedure in that worksheet module, not in a standard module.
Paste the following code, using straight quotation marks. Replace secret with the sheet password, and update the cell references, selection text, and row ranges as needed: Private Sub Worksheet_Change(ByVal Target As Range) Dim wasProtected As Boolean Dim wasUnprotected As Boolean Dim oldEvents As Boolean Dim errorMessage As String If Intersect(Target, Me.Range("C5,C7")) Is Nothing Then Exit Sub oldEvents = Application.EnableEvents On Error GoTo CleanUp Application.EnableEvents = False wasProtected = Me.ProtectContents If wasProtected Then Me.Unprotect Password:="secret" wasUnprotected = True End If Me.Rows("6:22").Hidden = True Select Case Me.Range("C5").Value Case "Annual Salary" Me.Rows("7").Hidden = False Select Case Me.Range("C7").Value Case "Option 1" Me.Rows("8:9").Hidden = False Case "Option 2" Me.Rows("10:12").Hidden = False End Select Case "Hourly Wage" Me.Rows("16").Hidden = False End Select CleanUp: If Err.Number <> 0 Then errorMessage = Err.Description On Error Resume Next If wasUnprotected Then Err.Clear Me.Protect Password:="secret" If Err.Number <> 0 Then errorMessage = errorMessage & " Sheet protection could not be restored: " & Err.Description End If End If Application.EnableEvents = oldEvents On Error GoTo 0 If Len(errorMessage) > 0 Then MsgBox errorMessage, vbExclamation End Sub
Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if prompted, then change C5 and C7 using their drop-downs and check the visible rows.

Edit Spreadsheet Forms with WPS Office
WPS Office is a free, lightweight alternative for working with spreadsheet forms in a familiar interface. Open compatible Excel files to make the move between office suites easier; test any VBA-dependent behavior separately because macro support can vary.

Frequently Asked Questions
Why does my Worksheet_Change macro not run?
Right-click the form's sheet tab and select View Code to confirm the procedure is in that sheet's module. Also check that macros are enabled, the monitored cell matches your drop-down, and the procedure is named Worksheet_Change.
How do I change which rows appear for each selection?
In the worksheet code, edit the Case labels to match the drop-down text and change the row numbers in Me.Rows(...).Hidden = False. Update the initial Rows("6:22") range to cover only the rows the form should control.
Can the drop-down be changed while the worksheet is protected?
Yes, if the drop-down cell is unlocked before protection is applied. Select the cell, open Format Cells > Protection, clear Locked, and then protect the sheet using Review > Protect Sheet.
Will this event run when a formula changes C5?
No. Worksheet_Change responds to cell edits, including drop-down selections, but not to a formula result changing during recalculation. A formula-driven form needs a different approach, such as a Worksheet_Calculate event.
Why does Excel warn me when I save the workbook?
VBA code cannot be retained in a standard XLSX workbook. Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm) to keep the event procedure.




