Running a macro in current sheet and in all sheets after current one

7,648

Something like this?

  1. RunMacroOnAllSheetsToRight loops through every sheet from the active sheet to the last sheet
  2. For those sheets the function MyFunction is called which gets the sheet index as argument. The Msgbox is just an example action. Change it to your needs.

Sub RunMacroOnAllSheetsToRight()
    For i = ActiveSheet.Index To Sheets.Count
        Call MyFunction(i)
    Next i
End Sub

Function MyFunction(i)
    'Code goes here
    MsgBox "I'm currently on sheet " & ThisWorkbook.Sheets(i).Name
End Function
Share:
7,648

Related videos on Youtube

isklenar
Author by

isklenar

Updated on September 18, 2022

Comments

  • isklenar
    isklenar over 1 year

    I have a excel file, that has 12 sheets, each corresponding to a month. I have three macros that execute in a sheet they are run in. Is is possible to do it in a way, that if I run macro in sheet October, then it will also run in sheet November and December?

    To put it more "mathematically". I have n sheets. I execute macro in sheet k (k > 0 && k <= n) and I want that same macro to execute in sheets < k+1, k+2, ... , n >.

    • CharlieRB
      CharlieRB over 10 years
      Please tell us, in a non "mathematical" way, what you've tried so far. You said 3 macros on each sheet, then asked to run Octobers macro on November & December. Are the macros the same? Or do you mean to have October trigger the other macros?
    • Raystafarian
      Raystafarian over 10 years
      Agree, it's not very clear. If you need the same macro to run, you just do for ws loop. If you need different macros to run then you need an if sheetx then call nest. What have you tried so far?