0

I want to run a code on multiple sheets. The sheet names are: Sheet1, Sheet2, 1, 2 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 and Summary. I define my sheets of interest= (1, 2, 3, 4 and Summary). The code should run only on these sheets. If any sheet in sheets of interest is not present, it should run for all other sheet of interest, i.e. if 1,2 are not present it should run for 3,4 and Summary.

Community
  • 1
  • 1
Curious Lad
  • 427
  • 2
  • 6
  • 13
  • possible duplicate of [Excel Macro : loop through Excel Sheets](http://stackoverflow.com/questions/20422356/excel-macro-loop-through-excel-sheets) – moffeltje Jul 13 '15 at 09:17
  • BTW, you question is not very clear. Could you give some examples? – moffeltje Jul 13 '15 at 09:40
  • Ok, I will make it clear. Suppose a workbook contains following sheets: Sheet1, Sheet2, 1, 2 ,3 ,4 ,5......12, Summary. Now, I want the code to run for 1, 2, 3, 4 and Summary. If any sheet out of 1, 2, 3, 4 and Summary doesn't exist, the code should be able to run for other sheets(If 2, 3 are not present, the code should run for 1,4 and Summary). I hope this makes it clear. – Curious Lad Jul 13 '15 at 09:44
  • So you want to run your code for every sheet except sheets with the name `"Sheetx"` where `x` is a number? – moffeltje Jul 13 '15 at 09:46
  • No, I want to run it for the sheet name I can store somewhere. Like if sheetname= 1, 2, 3, 4 and Summary, then run for every sheet found out of these sheets. – Curious Lad Jul 13 '15 at 09:48
  • I'm sorry your explanation just doesn't make sense to me so i'm out. – moffeltje Jul 13 '15 at 09:50
  • Ok, I will give it a try last time. Workbook: Sheet1, Sheet2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 and Summary are sheet names. Now, if I define my desired sheets= (1, 2, 3,4 and Summary), then it should run only for 1, 2, 3, 4 and Summary even if any of the sheets in my desired sheet is not present. If 2,3 is not present my code should run for the rest, i.e. 1, 4 and Summary. – Curious Lad Jul 13 '15 at 09:55

4 Answers4

1

you can loop each sheets into your workbook

Option Explicit
Dim ws As Worksheet, a As Range
Sub forEachWs()

For Each ws In ActiveWorkbook.Worksheets
    Call yourcode
Next

End Sub
Fabrizio
  • 662
  • 1
  • 7
  • 23
0

This is a very basic question and you should find the answer by simply Googling it. Though here combined with this is the answer for you.

Sub WorksheetLoop()

     Dim WS_Count As Integer
     Dim I As Integer
     Dim found As Integer
     Dim index As Integer
     Dim sheetnames {"1", "2", "Summary"}



     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = ActiveWorkbook.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count
         found = 0;

        For index = 0 To numbers.GetUpperBound(0)
            If sheetnames(index) = ActiveWorkbook.Worksheets(I).Name Then
                found = 1
            EndIf
        Next 

        If found = 1 Then

            ' Insert your code here.
            ' The following line shows how to reference a sheet within
            ' the loop by displaying the worksheet name in a dialog box.
            MsgBox ActiveWorkbook.Worksheets(I).Name
        EndIf

     Next I

End Sub
moffeltje
  • 4,521
  • 4
  • 33
  • 57
0

As an alternative to For...Next Loop this will work on all worksheets that are numbered:

Sub AllSheets()

    Dim wrkSht As Worksheet

    For Each wrkSht In ThisWorkbook.Worksheets
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Or any workbook.worksheets reference:                   '
    'For Each wrkSht In ActiveWorkbook.Worksheets            '
    'For Each wrkSht In Workbooks("Book2.xlsx").Worksheets   '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If IsNumeric(wrkSht.Name) Then
            'Your code here.
        End If
    Next wrkSht

End Sub

As a complete alternative you could put the required sheet names in a range in the workbook, give the range a defined name and use this:

Sub All()

    Dim rCell As Range
    Dim wrkSht As Worksheet

    For Each rCell In Range("MyDefinedSheetNameRange")
        If WorkSheetExists(rCell.Value) Then
            Set wrkSht = ThisWorkbook.Worksheets(rCell.Value)
            'Do stuff with wrksht
        End If
    Next rCell

End Sub

Public Function WorkSheetExists(SheetName As String) As Boolean
    Dim wrkSht As Worksheet
    On Error Resume Next
        Set wrkSht = ThisWorkbook.Worksheets(SheetName)
        WorkSheetExists = (Err.Number = 0)
        Set wrkSht = Nothing
    On Error GoTo 0
End Function
Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
  • See, my point is if I store the name of sheets somewhere, the code should run for all the sheets that exist irrespective if the sheet is numbered or have some name. Can you help with that? – Curious Lad Jul 13 '15 at 09:28
  • If you want all worksheets then use @Fabrizio which will reference all worksheets irrespective of the name, mine will work on just numbered. – Darren Bartrup-Cook Jul 13 '15 at 09:31
  • I think you didn't get what I asked clearly. Suppose there is a workbook containing sheets named as : Sheet1, Sheet2, 2, 3, 4, 5, Summary. So, if I run the code it should run on 2, 3, 4, 5 and Summary. The name of the sheet I want the macro to run can be stored somewhere. Even if any of the sheet does not exist, the code should run for other sheet whose name is stored. – Curious Lad Jul 13 '15 at 09:35
0
For Each sht In ThisWorkbook.Sheets
    If sht.Name <= 12 Then
     '
     '
     'MsgBox sht.Name
    End If
Next
user3364224
  • 11
  • 3
  • 7
  • A good answer should include some explanation of the code to show how it solves the problem presented in the question. This answer is nothing but a few lines of code with no explanation. Consider expanding this answer to show how it solves the problem as described, – psubsee2003 Jul 14 '15 at 23:15