VBA & Macro Problems

Excel VBA Automatically Hide and Unhide Employee Rows

Olivia MillerOlivia Miller Nov 12, 2026 868 views

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.

Automatically Hide and Unhide Employee Rows with Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the worksheet code module

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.

2
Paste the row visibility macro

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

3
Save the workbook with macro support

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.

4
Test termination and rehire updates

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.

Add a Worksheet_Change VBA macro to manage employee row visibility
Automatic visibility is enabled: The macro runs whenever a value changes in columns D or E. It also restores application events if an error occurs during processing.
Employee worksheet automation

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. 1. Open the employee workbook: Launch WPS Spreadsheets and open the employee list saved as an .xlsm workbook.
  2. 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. 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. 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.
Automate hiding and unhiding employee records based on termination and rehire dates.Use a familiar spreadsheet interface for editing rows, dates, and VBA-style automation.Open and save Microsoft Excel macro-enabled workbook formats, including .xlsm files.Maintain compatibility with Microsoft Excel .xlsx and .xlsm employee worksheets.
QA img-9

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.