Here's a similar version that will do the task automatically when cell B2 is changed.
By using SELECT / CASE structure instead of IF / ELSEIF, you can easily add more values if needed.
Place this in the code section for the sheet you are working with.
Private Sub Worksheet_Change(ByVal Target As Range)
' For Excel Forum, Aug 3, 2015; user A440.
' I need to hide column f or column g based on text in cell b2, I would like the 2 column to auto switch if possible please.
' If "B2" contains "Darrell" so hide column "G" and un hide column "F".
' If "B2" contains "Brendon" hide column "F" and un hide column "G".
' If "B2" is empty un hide "F" & "G"
If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Select Case Target
Case Is = "Darrell"
Columns("G").Hidden = True
Columns("F").Hidden = False
Case Is = "Brendon"
Columns("G").Hidden = False
Columns("F").Hidden = True
Case Else
Columns("G").Hidden = False
Columns("F").Hidden = False
End Select
Application.EnableEvents = True
End Sub
Bookmarks