Excel VBA Automatically Hide and Unhide Employee Rows
Question details
Create a worksheet VBA macro that automatically hides an employee row after a termination date is entered and shows the row again when the termination date is removed or the employee is rehired.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- Employee names are stored in column C, termination dates are entered in column D, and optional rehire dates can be entered in column E.
- Observed behavior
- The employee row should be hidden when a valid termination date exists without a rehire date, and visible again when the termination date is cleared or a rehire date is entered.
Confirm that employee records begin on row 2 and that columns C, D, and E contain employee names, termination dates, and rehire dates. Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) and enable macros when reopening it.
Add a Worksheet_Change VBA macro to manage employee row visibility
Use this solution when termination or rehire dates are entered directly on the employee worksheet and row visibility should update automatically.
This event macro checks changed cells in the termination and rehire date columns. A row is hidden only when column D contains a valid termination date and column E does not contain a valid rehire date.
In Excel, press Alt+F11 to open the Visual Basic Editor. In Project Explorer, double-click the worksheet that contains the employee list, such as Sheet1.
Paste the following code into that worksheet module: Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Range Dim lastRow As Long If Intersect(Target, Me.Range("D:E")) Is Nothing Then Exit Sub On Error GoTo SafeExit Application.EnableEvents = False lastRow = Me.Cells(Me.Rows.Count, "C").End(xlUp).Row For Each cell In Me.Range("D2:D" & lastRow) cell.EntireRow.Hidden = IsDate(cell.Value) And _ Not IsDate(Me.Cells(cell.Row, "E").Value) Next cell SafeExit: Application.EnableEvents = True End Sub
Select File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and save the file. Close and reopen it if Excel asks you to enable macros.
Enter a date in column D for an employee to hide that row. Clear the date in column D to show the row again, or enter a valid rehire date in column E while retaining the termination history.

Use termination dates only when no rehire-date column is needed
Use this simpler approach if clearing the termination date is the only way to reactivate an employee record.
Manage Employee Row Visibility in WPS Spreadsheets
WPS Spreadsheets can help you maintain employee lists with familiar spreadsheet tools and VBA-compatible macro workflows where VBA support is available. Use the same termination and rehire date logic to automate row visibility in a macro-enabled workbook.
- 1. Open the employee workbook: Launch WPS Spreadsheets and open the employee list saved as an .xlsm workbook.
- 2. Check the employee columns: Verify that employee names are in column C, termination dates are in column D, and rehire dates, if used, are in column E.
- 3. Access macro tools: Open the Developer or Macro tools available in your WPS Spreadsheets edition, then add or manage the worksheet macro for the employee sheet.
- 4. Enable macros and test updates: Enable macros when prompted, enter a test termination date in column D, and confirm that clearing it or entering a rehire date restores the row.

Frequently Asked Questions
Where should I paste the Worksheet_Change macro?
Paste it into the code module for the specific employee worksheet, not into a standard module. Press Alt+F11, locate the worksheet under Microsoft Excel Objects, and double-click it before pasting the code.
Why does the employee row not hide after I enter a termination date?
Check that macros are enabled, the date is entered in column D, and the code is in the correct worksheet module. Also verify that the value is recognized as a real date rather than text.
How can I show an employee again after rehiring them?
Clear the termination date in column D, or use the version that checks column E and enter a valid rehire date there. The macro sets the row back to visible when it finds a rehire date.
Can a formula or conditional formatting hide employee rows automatically?
No. Formulas and conditional formatting can flag or highlight terminated employees, but they cannot reliably change row visibility automatically. Use VBA for automatic hiding and unhiding.
What happens if several employee dates are pasted at once?
The Worksheet_Change event runs after the paste and loops through the employee list, updating each row based on its termination and rehire date values.




