How to Hide or Unhide Excel Columns Using a Drop-Down List and VBA
Question details
The user needs to dynamically control column visibility in a worksheet based on the selection made from a drop-down list using a VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Managing large datasets where users only need to view data for specific categories, such as selecting an employee's name from a drop-down to display only their corresponding data columns while hiding the rest.
- Observed behavior
- Excel normally requires manual hiding and unhiding of columns. The user wants to automate this process so that changing a data validation drop-down selection instantly updates which columns are visible on the sheet.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) so the VBA code will run, and verify that your Developer tab is visible to easily access the Visual Basic Editor.
Automate Column Visibility Using a Named Range and Application.Match
This method uses a named range for your column headers and a specific cell for the drop-down, providing a clean and easily adjustable VBA script that triggers automatically upon cell change.
By utilizing the Worksheet_Change event, Excel will constantly monitor the drop-down cell. Once a new value is selected, the macro matches the value against a named range, hides all columns in that range, and then unhides only the specific column that matches.
Select your control cell (e.g., G2), go to the Data tab, click Data Validation, and create a List containing your employee names or categories.
Highlight your data column headers, click into the Name Box (next to the formula bar), type 'Employee_List', and press Enter.
Right-click the current worksheet tab at the bottom of your screen and select 'View Code' to open the Visual Basic Editor for that specific sheet.
Paste the following logic into the module: Set your range variable, use Application.Match to find the selected value, set rgList.EntireColumn.Hidden = True, and then set the matched column's EntireColumn.Hidden = False.
Close the VBA editor and change the selection in your drop-down list in cell G2. The corresponding columns should now hide and unhide automatically.

Loop Through Specific Column Letters to Toggle Visibility
If your columns are contiguous and easily defined by a string of letters, this loop-based approach allows you to check each column header directly against the drop-down selection.
Automate Your Spreadsheets with WPS Office
WPS Spreadsheet offers robust support for VBA macros and advanced data validation, allowing you to easily automate tasks like dynamically hiding and unhiding columns.
- 1. Set Up Data Validation: Open your workbook in WPS Spreadsheet, navigate to the 'Data' tab, and use Data Validation to create your drop-down list.
- 2. Access the Developer Tools: Go to the 'Developer' tab on the ribbon and click 'VBA Editor' to access the coding environment.
- 3. Add Your Macro: Insert your Worksheet_Change code into the corresponding sheet module, then save your file as a Macro-Enabled Workbook (.xlsm) to preserve the automation.

Frequently Asked Questions
Why isn't my VBA code triggering when I change the drop-down list?
This usually happens if macros are disabled or if the code is placed in a standard Module instead of the specific Sheet module. Ensure you right-click the sheet tab, select 'View Code', paste the script there, and check your macro security settings.
Can I unhide all columns if the drop-down list is left blank?
Yes, you can modify the VBA script to include a condition that checks if the target cell value is empty (e.g., If Target.Value = "" Then). If it is empty, you can set the EntireColumn.Hidden property to False for your entire controlled range.
Does this VBA method work in WPS Office?
Yes, WPS Office Spreadsheet supports VBA macros. You can use the exact same Worksheet_Change event code to hide and unhide columns dynamically, provided you are using a version of WPS Office with the VBA module installed.




