Hi Forum,
Please find attached an example of what i'm trying to achieve (note this is a small portion of a much larger sheet).
Essentially whenever a 'Y' or a 'N' is entered into a cell the macro would check a range of cells and only hide the columns if no other 'Y' 's are found in the range.
This needs to then be replicated for a couple of ranges within the one worksheet. (see attached)
I guessed the below which does not work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Dim Options1 As Range
For Each Options1 In Range("A5:A30")
If Options1.Value = "y" Then
Columns("B:C").EntireColumn.AutoFit
ElseIf Options1.Value = "Y" Then
Columns("B:C").EntireColumn.AutoFit
Else
Columns("B:C").ColumnWidth = 0
End If
Exit For
Next Options1
'ElseIf Target.Column = 5 Then
' Dim Options2 As Range
' For Each Options2 In Range("I46:I71")
' If Options2.Value = "y" Then
' Columns("J:K").EntireColumn.AutoFit
' ElseIf Options2.Value = "Y" Then
' Columns("J:K").EntireColumn.AutoFit
' Else
' Columns("J:K").ColumnWidth = 0
' End If
' Exit For
' Next Options2
'ElseIf Target.Column = 9 Then
' Dim Options3 As Range
' For Each Options3 In Range("I46:I71")
' If Options3.Value = "y" Then
' Columns("J:K").EntireColumn.AutoFit
' ElseIf Options3.Value = "Y" Then
' Columns("J:K").EntireColumn.AutoFit
' Else
' Columns("J:K").ColumnWidth = 0
' End If
' Exit For
' Next Options3
'ElseIf Target.Column = 13 Then
' Dim Options4 As Range
' For Each Options4 In Range("I46:I71")
' If Options4.Value = "y" Then
' Columns("J:K").EntireColumn.AutoFit
' ElseIf Options4.Value = "Y" Then
' Columns("J:K").EntireColumn.AutoFit
' Else
' Columns("J:K").ColumnWidth = 0
' End If
' Exit For
' Next Options4
Else
Exit Sub
End If
End Sub
Many thanks in advance.
-Ollie
Bookmarks