Looping through sheets and workbooks using array

  • This is grinding me down and appears to be a simple issue. I'm trying to loop through 4 workbooks that are opened alongside other books, then from the same named sheets from each of the 4, copy rows. It's not working. Any thoughts would be appreciated.


  • Re: Looping through sheets and workbooks using array


    That code will certainly error because you need a '&' after '(Cnt)' in your first loop, and in your second the next variable is different from the For. Are they just typos? Exactly how is it not working?

  • Re: Looping through sheets and workbooks using array


    Yes Stephen they are just typos caused by hacking out loads of irrelevant code.
    It is the Set School_index = WB_temps etc that doesn't work. The Sheets part is ok, but it doesn't like the WB_temps(Cnt) part.
    Thanks
    Mark

  • Re: Looping through sheets and workbooks using array


    I can't say I've ever used arrays in that way. Do the workbooks definitely have sheets with those names?


    You need to say what you mean by "doesn't work" - does it error, if so what error and on what line? Does it run but not work correctly?

  • Re: Looping through sheets and workbooks using array


    No Stephen, I have changed the sheet names but the logic is the same and they've worked before with an array.
    It appears to be the Workbook part that is causing the problem. Actually I've been trying a number of solutions and mistakenly put the wrong part in error - apologies for that (the irritation is affecting my focus). It is the line:

    Set WB_temps(Cnt) = Workbooks.Open("schools_data_" & (Cnt)& ".xlsx")


    that is causing an error, which is noted as <Subscript out of range> after first opening successfully the first workbook.


    Apologies again,
    Mark

  • Re: Looping through sheets and workbooks using array


    Stephen, I solved it - eventually. It required a redim statement as well as defining the array for WB_temps. Thanks for your input.

Participate now!

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