Repeat vba code for specific array of worksheets

  • Hi,


    I'm trying to figure out how have the below code re-run through a certain array of worksheets in the same workbook.

    From the following code I need to loop through an array of worksheet names performing the same vba code each time:


    Code
    Sheets(Array("Sheet1", "Sheet2")).Select 
    
    
    For Each worksheet In Array
    
    
    Next worksheet In an array


    Can anybody help me out with this one please?

    Here is the full code:


    Kind regards,

    05125668

  • Re: Repeat vba code for specific array of worksheets


    Like this:


    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: Repeat vba code for specific array of worksheets


    Hi,


    Thanks very much for the reply.


    I have tried as you have suggested below, however I receive the following error "Complie Error: For Each control variable must be Variant or Object" and the "wsName" text is highlighted.


    Code
    Sub test()
        Dim cmonth As Date, cfind As Range, pmonth As Date, nmonth As Date, ws As Worksheet, wsName As String
        With Worksheets("Dashboard")
            cmonth = .Range("J2").Value
            pmonth = .Range("K2").Value
            nmonth = .Range("I2").Value
         
            For Each wsName In Array("Sheet1", "Sheet2")
            Set ws = Worksheets(wsName)


    I have tried changing the wsName to Variant and Object however I get the following message when this is done "Run-time error "9": Subscript out of range"


    Code
    Sub test()
        Dim cmonth As Date, cfind As Range, pmonth As Date, nmonth As Date, ws As Worksheet, wsName As Variant
        With Worksheets("Dashboard")
            cmonth = .Range("J2").Value
            pmonth = .Range("K2").Value
            nmonth = .Range("I2").Value
         
            For Each wsName In Array("Sheet1", "Sheet2")
            Set ws = Worksheets(wsName)


    Do you know why I will be receiving these error messages?


    Kind regards,

  • Re: Repeat vba code for specific array of worksheets


  • Re: Repeat vba code for specific array of worksheets


    Apologies, it should have read:


    Code
    Dim wsName As Variant

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: Repeat vba code for specific array of worksheets


    I still seem to get the "Run-time error '9': Subscript out of range" error when modifying the code as you mentioned. Do you have any other ideas on this?




    Thanks again for looking at this.

  • Re: Repeat vba code for specific array of worksheets


    The change in my previous post is the ONLY change you had to do.


    Specifically, your array of worksheets is incorrect. Please look at my first code again, with the change I suggested.

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!