VBA tweak required

bakerman

Board Regular
Joined
Sep 9, 2005
Messages
188
I am trying to hide columns when a cell is changed. The Cell "C6" contains a list populated through data validation. I have put the following code in to worksheet change but the code runs when any cell is changed and i only want it to run when cell "C6" is changed
any ideas?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
ActiveSheet.Unprotect
For Each c In Range("E1:AZ1")
If c.Value Like "0" Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next c
Application.ScreenUpdating = True
ActiveSheet.Protect
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$6" Then
'your code here
End If
End Sub
 
Upvote 0
Try

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) <> "C6" Then Exit Sub
Application.ScreenUpdating = False
ActiveSheet.Unprotect
For Each c In Range("E1:AZ1")
If c.Value Like "0" Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next c
Application.ScreenUpdating = True
ActiveSheet.Protect
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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