VBA - Code to hide column(s) not working

mark9988

Board Regular
Joined
Sep 30, 2005
Messages
90
Hello -

I have an IF statement formula in cell C:515 that produces a value of 1, 2, 3, 4, or 5. My issue is that the code below does not hide/unhide the corresponding columns based on the formula in cell C:515.

However, when I manually type a value in cell C:515, the code works perfectly fine. Any thoughts on why this code is not working with the IF statement ?

Thanks!!



Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = ("$C$515") Then
If Target.Text = "1" Then
Columns("H:H").EntireColumn.Hidden = False
Columns("D:G").EntireColumn.Hidden = True

ElseIf Target.Text = "2" Then
Columns("G:H").EntireColumn.Hidden = False
Columns("D:F").EntireColumn.Hidden = True

ElseIf Target.Text = "3" Then
Columns("F:H").EntireColumn.Hidden = False
Columns("D:E").EntireColumn.Hidden = True

ElseIf Target.Text = "4" Then
Columns("E:H").EntireColumn.Hidden = False
Columns("D:D").EntireColumn.Hidden = True

ElseIf Target.Text = "5" Then
Columns("D:H").EntireColumn.Hidden = False

End If
End If
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try changing
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
to:
VBA Code:
Private Sub Worksheet_Calculate()
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Calculate()
   Range("D:H").EntireColumn.Hidden = True
   With Range("C515")
      If .Value > 0 Then
         Range("H:H").Offset(, -.Value + 1).Resize(, .Value).EntireColumn.Hidden = False
      End If
   End With
End Sub
But this will run whenever any cell on the sheet is recalculated.
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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