Display/Hide Columns Based on Drop Down List Selection

JoRyBar

New Member
Joined
Aug 19, 2018
Messages
17
Hello,

I am currently working on displaying and hiding all other columns in a range based on a drop down. In row 7 I have all the columns identified by text, which is the same as the drop down list. When an user selects the drop down, the sheet should hide all other columns except the one selected. The following code does not seem to work:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim R, V
If Target.Address = ("$f$3") Then
V = [F3].Value
For Each R In Range("J7:FV7")
If IsError(R.Value) Then
R.EntireColumn.Hidden = True
Else
R.EntireColumn.Hidden = R.Value <> V
End If
Next
End If
End Sub

I am not sure if it is because I have other VBA codes on the sheet or if the above is bad coding. I seem to always have issue with Private Sub Worksheet_Change(ByVal Target As Range) code.... Not sure if that is the issue....

Help Gurus!

Thanks!

Novice
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Fnd As Range
   If Target.Address(0, 0) = "F3" Then
      Range("J:FV").EntireColumn.Hidden = True
      Set Fnd = Range("F7:FV7").find(Target.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then Fnd.EntireColumn.Hidden = False
   End If
End Sub
 
Upvote 0
Hi Fluff,

I get an error that points to Private Sub Worksheet_Change(ByVal Target As Range). It says "Compile error: Ambiguous name detected: Worksheet_Change"

I am not sure how to fix this.
 
Upvote 0
You need to remove the code that you already have.
 
Upvote 0
Hi,

I need the other code for date stamping (see below):

Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("S:S,U:U"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub

Is there any way we can make it so that both codes can work on the same sheet?

Thanks!
 
Upvote 0
try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim WorkRng As Range
   Dim Rng As Range, Fnd As Range
   
   Set WorkRng = Intersect(Range("S:S,U:U"), Target)
   If Not WorkRng Is Nothing Then
      Application.EnableEvents = False
      For Each Rng In WorkRng
         If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, 1).Value = Now
            Rng.Offset(0, 1).NumberFormat = "dd-mm-yyyy, hh:mm"
         Else
            Rng.Offset(0, 1).ClearContents
         End If
      Next
      Application.EnableEvents = True
   End If
   If Target.Address(0, 0) = "F3" Then
      Range("J:FV").EntireColumn.Hidden = True
      Set Fnd = Range("F7:FV7").find(Target.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then Fnd.EntireColumn.Hidden = False
   End If
End Sub
 
Upvote 0
Almost! But it only shows 1 out of 13 columns. Each drop down task (which there are 12 choices) has 13 associated columns. The formula only shows the first of the columns, but hides the other 12.

Also, is there a way that all the columns can unhide when the drop down is blank?

Thank you so much for your speedy responses!!
 
Upvote 0
Maybe
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim WorkRng As Range
   Dim Rng As Range, Cl As Range

   Set WorkRng = Intersect(Range("S:S,U:U"), Target)
   If Not WorkRng Is Nothing Then
      Application.EnableEvents = False
      For Each Rng In WorkRng
         If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, 1).Value = Now
            Rng.Offset(0, 1).NumberFormat = "dd-mm-yyyy, hh:mm"
         Else
            Rng.Offset(0, 1).ClearContents
         End If
      Next
      Application.EnableEvents = True
   End If
   If Target.Address(0, 0) = "F3" Then
      If Target.Value = "" Then
         Range("J:FV").EntireColumn.Hidden = False
      Else
         For Each Cl In Range("J7:FV7")
            Cl.EntireColumn.Hidden = Cl.Value <> Target.Value
         Next Cl
      End If
   End If
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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