Greetings,
I currently have two macros in my workbook, one to Hide Columns based on values in row 5, and the other to Unhide Columns based on values in row 5.
I am trying to have these macros run automatically based on dynamic changes in values in row 5. In row 5, I have formulas that populate the cells with "Hide" and "Unhide". As the values in row 5 for specific cells change from "Hide" to "Unhide", I'd like for the whole columns to automatically hide or be unhidden. To accomplish this, I am trying the following code, but keep getting a Type Mismatch error.
I would really appreciate help understand what I am doing wrong and fixing the code.
Thanks so much!
I currently have two macros in my workbook, one to Hide Columns based on values in row 5, and the other to Unhide Columns based on values in row 5.
VBA Code:
Sub Hide_Columns()
'
' Hide_Columns Macro
'
Dim c As Range
For Each c In Rows("5:5").Cells
If c.Value = "Hide" Then
c.EntireColumn.Hidden = True
'You can change the property above to False
'to unhide the columns.
End If
Next c
'
End Sub
VBA Code:
Sub Unhide_Columns()
'
' Hide_Columns Macro
'
Dim c As Range
For Each c In Rows("5:5").Cells
If c.Value = "Unhide" Then
c.EntireColumn.Hidden = False
'You can change the property above to False
'to unhide the columns.
End If
Next c
'
End Sub
I am trying to have these macros run automatically based on dynamic changes in values in row 5. In row 5, I have formulas that populate the cells with "Hide" and "Unhide". As the values in row 5 for specific cells change from "Hide" to "Unhide", I'd like for the whole columns to automatically hide or be unhidden. To accomplish this, I am trying the following code, but keep getting a Type Mismatch error.
VBA Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("A5:XFD5")
If target.Value = "Hide" Then
Call Hide_Columns
End If
If target.Value = "Unhide" Then
Call Unhide_Columns
End If
End Sub
I would really appreciate help understand what I am doing wrong and fixing the code.
Thanks so much!