Hide columns based on cell value

cafeannalisa

New Member
Joined
Apr 29, 2017
Messages
9
Office Version
  1. 2016
Platform
  1. MacOS
I have much the same question as but can't get the answers to work
Columns J1-N1 have headers 1 2 3 4 5
I want to hide columns based on S1, which takes values 1-5
If S1 = 1 Hide columns headed 2-5 i.e. K:N
If S1 = 2 Hide columns headed 3-5 i.e.L:N
If S1 = 3 Hide columns headed 4-5 i.e.M:N
If S1 = 4 Hide column headed 5 i.e. N
[If S1 = 5 No hiding]
I have tried a version of alansidman's code on the earlier thread without success.

Private Sub Worksheet_Change(ByVal Target As Range)

Set Target = Range(“S1")
If Target = 1 Then
columns(“K:N”).Hidden = True
ElseIf Target = 2 Then
columns("K:N").Hidden = False
columns(“L:N").Hidden = True
ElseIf Target = 3 Then
columns("K:N").Hidden = False
columns(“M:N").Hidden = True
ElseIf Target = 4 Then
columns("K:N").Hidden = False
columns(“N”).Hidden = True
End If
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
In what way doesn't it work?
 
Upvote 0
Ok, how is S1 being changed?
 
Upvote 0
Ok, in that case is the code in the relevant sheet module, or in a standard module?
 
Upvote 0
Ok, in that case is the code in the relevant sheet module, or in a standard module?
i think it is a standard module - first time i have done this. Its in a 'Modules' folder at top level in the Project Sidebar
 
Upvote 0
Ok, you need to put the code in the relevant sheet module, in Xl right click the tab of the sheet you want the code to work on & select view code, then paste the code into the window that opens up.
 
Upvote 0
Solution
You could also use this code, which does the same thing
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "S1" Then
      Range("K:N").EntireColumn.Hidden = False
      Select Case Target
         Case 1 To 4
            Cells(1, Target + 10).Resize(, 5 - Target.Value).EntireColumn.Hidden = True
      End Select
   End If
End Sub
 
Upvote 0
Ok, you need to put the code in the relevant sheet module, in Xl right click the tab of the sheet you want the code to work on & select view code, then paste the code into the window that opens up.
Yes thanks so much for a New Year breakthrough, I learned something crucial obviously!
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,533
Members
448,969
Latest member
mirek8991

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