简体   繁体   中英

Excel VBA will not loop through worksheets (Exclude Worksheet)

The macro appears to loops through all of the worksheets fine now. However, is there a way that I can make it so the macro does not get applied to a specific worksheet in my workbook, but does get applied to all other worksheets?

Sub FormatSheet()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
        Columns("A:J").Select
        Selection.AutoFilter
        Columns("A:J").EntireColumn.AutoFit
        With Selection
            .HorizontalAlignment = xlCenter
        End With
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True
Next ws

End Sub

I created a dataset to test this out and found that the code worked just fine. So it must be something related to your specific data/worksheets. I would see if 2 sheets works, or try making a smaller sample on another workbook and see if it works. Sorry I'm not more helpful.

Consider this approach.

Option Explicit

Sub test()

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" Then
            With ws.UsedRange
                .Resize(.Rows.Count-1).Offset(1, 0).ClearContents
            End With
        End If
    Next ws

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM