Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Code to cycle through worksheets starting with a specific sheet (index 3)

Tags:

excel

vba

I need to cycle through sheets of index 3 tip last sheet and run a code. I tried something like this but its doesn't work.

If (ws.sheetIndex > 2) Then
        With ws
            'code goes here
        End With
End If

I did a search but don't find a solution to this problem. Help would be much appreciated.

I also tried:

Dim i As Long, lr As Long
Dim ws As Worksheet
Windows("Book1").Activate

With ActiveWorkbook
    Set ws = .Worksheets("index")
    For i = 3 To 10
        'code goes here
    Next i


End With
like image 916
user3720702 Avatar asked Jul 19 '14 23:07

user3720702


3 Answers

You can try the following, which iterates over all worksheets in your workbook and only "acts" for worksheets with index 3 or above.

Dim sheet As Worksheet

For Each sheet In ActiveWorkbook.Worksheets
  If sheet.Index > 2 Then
    ' Do your thing with each "sheet" object, e.g.:
    sheet.Cells(1, 1).Value = "hi"
  End If
Next

Note that this doesn't put a hard limit on the number of sheets you have (10 or whatever), as it will work with any number of worksheets in your active workbook.


EDIT

If you want the code to run on worksheets with names "Sheet" + i (where i is an index number from 3 onwards), then the following should help:

Dim sheet As Worksheet
Dim i As Long

For i = 3 To ActiveWorkbook.Worksheets.Count
  Set sheet = ActiveWorkbook.Worksheets(i)
  If sheet.Name = "Sheet" & i Then
    ' Do your thing with each "sheet" object, e.g.:
    sheet.Cells(2, 2).Value = "hi"
  End If
Next i

Of course, this means that the names of your worksheets need to always follow this pattern, so it's not best practice. However, if you're sure the names are going to stay like this, then it should work well for you.

like image 175
djikay Avatar answered Nov 14 '22 21:11

djikay


Try excluding first and second sheet using name:

Public Sub Sheets3andUp()
Dim ws As Worksheet
Dim nameOfSheet1 As String
Dim nameOfSheet2 As String

nameOfSheet1 = "Sheet1"
nameOfSheet2 = "Sheet2"

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> nameOfSheet1 And ws.Name <> nameOfSheet2 Then
        'Code goes here
        Debug.Print ws.Name
    End If
Next ws

End Sub

like image 22
osquro Avatar answered Nov 14 '22 22:11

osquro


Note that the user can reorder the sheets in the Worksheets Collection, so it is better to refer to the sheets by CodeName (which the user cannot change), and exclude by CodeName the sheets to be skipped, as here:

Public Sub TestLoop()
    On Error GoTo ErrHandler
    Dim ws As Worksheet, s As String
    For Each ws In Worksheets
        If ws.CodeName <> "Sheet2" Then
            s = s & vbNewLine & ws.CodeName
        End If
    Next ws

    s = "WorksheetList (except Sheet2:" & vbNewLine & vbNewLine & s

    MsgBox s, vbOKOnly, "Test"
EndSUb:
    Exit Sub
ErrHandler:
    Resume EndSUb
End Sub

If I drag Sheet 3 to precede Sheet1, the MsgBox outputs:

WorksheetList (except Sheet2:

Sheet3
Sheet1
like image 30
Pieter Geerkens Avatar answered Nov 14 '22 23:11

Pieter Geerkens