简体   繁体   中英

Skip first worksheet of the workbook in VBA

I want to have a number of worksheet on each of worksheets in a workbook skipping the first one and do some formatting as well, however i want this vba code to skip the first worksheet (name can differ but always is going to be first). Thus the question is how should i do that?

Sub ex2()
    Dim kl As Worksheet
    Dim Ws_Count As Integer
    Dim a As Integer
    Ws_Count = ActiveWorkbook.Worksheets.Count
    For a = 2 To Ws_Count

        With Rows("2:2")
            .RowHeight = 20
            .Interior.Color = RGB(150, 250, 230)
        End With
        With Range("B2")
            .Value = "Sheet Number" & " " & a
            .Font.Size = 12
            .Font.Bold = True
            .Font.Underline = True
        End With
    Next a
End Sub

Try this:

Sub ex2()

Dim Ws_Count As Integer
Dim a As Integer


Ws_Count = ActiveWorkbook.Worksheets.Count

For a = 2 To Ws_Count
    With Worksheets(a)
    'rest of your code
    End With
Next a
End Sub

With the posted code, the end result would be:

Sub ex2()
    Dim Ws_Count As Integer
    Dim a As Integer    


    Ws_Count = ActiveWorkbook.Worksheets.Count

     For a = 2 To Ws_Count
        With Worksheets(a)

        Worksheets(a).Activate
        With Rows("2:2")
            .RowHeight = 20
            .Interior.Color = RGB(150, 250, 230)
        End With
        With Range("B2")
            .Value = "Sheet Number" & " " & worksheets(a).Index - 1
            .Font.Size = 12
            .Font.Bold = True
            .Font.Underline = True
        End With
    Next a
End Sub

Your code was good, you were only missing a single line, checking the current sheet kl.Index .

Code

Option Explicit

Sub ex2()

    Dim kl As Worksheet

    For Each kl In Worksheets
        ' not the first worksheet
        If kl.Index > 1 Then

            With kl.rows("2:2")
                .RowHeight = 20
                .Interior.Color = RGB(150, 250, 230)
            End With
            With kl.Range("B2")
                .Value = "Sheet Number" & " " & kl.Index - 1
                .Font.Size = 12
                .Font.Bold = True
                .Font.Underline = True
            End With
        End If
    Next kl

End Sub

you were almost there since you only missed worksheet specification

you could either add a either add a Worksheets(a).Activate statement right after For a = 2 To Ws_Count one or, which is much better, wrap your formatting code in a With Worksheets(a) ... End With block, adding dots ( . ) before every range reference and have them refer to the currently referenced worksheet, as follows

Sub ex2()
    Dim a As Integer

    For a = 2 To Worksheets.Count
        With Worksheets(a) '<--| reference current index worksheet
            With .Rows("2:2") '<--| reference current worksheet row 2
                .RowHeight = 20
                .Interior.Color = RGB(150, 250, 230)
            End With
            With .Range("B2") '<--| reference current worksheet cell "B2"
                .Value = "Sheet Number" & " " & a
                .Font.Size = 12
                .Font.Bold = True
                .Font.Underline = True
            End With
        End With
    Next a
End Sub

So, no need for any If statement that would have worked only once: although it wouldn't affect performance significantly in this case it would be very inefficient from a purely coding point of view

Loop through your worksheets like this, and check the index property (which stores the worksheets location) to make sure it's not the first.

Public Sub test()
For Each ws In Worksheets
    If ws.Index > 1 Then
        'Formatting goes here
    End If
Next
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