A Better way of hiding colums based on cell values?

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
169
Office Version
  1. 365
Hi guys ,

I am sure there is a better way of hiding columns than the code below that i am using.

Basically I need to hide the columns based on the cell value of column and whatever row I am currently on and ignore the pervious cell value in column 20 value if that make sense.

The spreadsheet I am working on will be updated continuously.

The below code works fine but rather than me having to update continuously as new rows are added I am sure there is a better way of doing this.

Any help is greatly appreciated.

Thanks,


rivate Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 20 And Target.Row = 3 And Target.Value = "Yes" Then
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = False
End If

If Target.Column = 20 And Target.Row = 4 And Target.Value = "Yes" Then
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = False
End If


If Target.Column = 20 And Target.Row = 5 And Target.Value = "Yes" Then
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = False
End If

If Target.Column = 20 And Target.Row = 6 And Target.Value = "Yes" Then
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = False
End If

If Target.Column = 20 And Target.Row = 7 And Target.Value = "Yes" Then
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("U:AI").Select
Application.Selection.EntireColumn.Hidden = False
End If



End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,
try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 20 And Target.Row > 2 Then
        Application.Columns("U:AI").EntireColumn.Hidden = CBool(Target.Value = "Yes")
    End If
End Sub

and see if this does what you want.


Dave
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

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