Macro to Insert/Remove a Column Based on Selection from a Validation List

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,168
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet in which users select from a drop-down list from cell A2 in order to display calculations in column B. I created the drop-down list using Data Validation and it works fine. What I would like to do is have a macro that hides certain columns depending on which item the user chooses from the drop-down list in cell A2?

For example, if in cell A2, the user chooses "GL" from the drop-down list, then columns A, C through Q are visible, while column B is hidden. If a user selects "Fund" from the drop down list, then all columns from A through Q should be visible.

I think title of the post should say Hide/Unhide rather than insert/remove. If Mod can fix that that would be great.

Can someone assist with this code.

Thanks.
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I tried something like this, but I think I need cell A2 referenced somewhere or fix anything else as necessary.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Value
Case "GL"
ActiveSheet.Range("B").EntireColumn.Hidden = True
Case "Fund"
ActiveSheet.Range("B").EntireColumn.Hidden = False
End Select
Application.EnableEvents = True
End Sub

Note: FYI - I have a empty selection from the drop-down list too.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top