0

Hi all pretty new to VBA here and I am struggling to get my macro to work on multiple sheets. I basically want to hide and unhide columns "B-AB" if they have an "X" in row 8. Currently the macro only works on the active sheet and not the active workbook. Thanks!

Sub roll()

Dim sh As Worksheet
Dim c As Range


For Each sh In ActiveWorkbook.Sheets

With sh

Application.ScreenUpdating = False

Columns("B:AB").Select
Selection.EntireColumn.Hidden = False


        For Each c In Range("b8:ab8").Cells
            If c.Value = "X" Then
            c.EntireColumn.Hidden = True
            End If

        Next c
            End With

Next sh

Application.ScreenUpdating = True

End Sub
  • 1
    https://stackoverflow.com/questions/28439376/what-is-the-default-scope-of-worksheets-and-cells-and-range/28439984#28439984 You aren't using your With block – Tim Williams Aug 28 '18 at 22:14

1 Answers1

2

You need to add . in front of the range object that are assigned to the sheet noted in the with:

Sub roll()

Dim sh As Worksheet
Dim c As Range


For Each sh In ActiveWorkbook.Sheets

With sh

    Application.ScreenUpdating = False

    .Columns("B:AB").Hidden = False


        For Each c In .Range("b8:ab8").Cells
            If c.Value = "X" Then
                c.EntireColumn.Hidden = True
            End If
        Next c
    End With

Next sh

Application.ScreenUpdating = True

End Sub
Scott Craner
  • 148,073
  • 10
  • 49
  • 81