+ Reply to Thread
Results 1 to 24 of 24

Need help with loops and arrays in VBA Macro

  1. #1
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Need help with loops and arrays in VBA Macro

    Hello,

    I need a macro to run on the active sheet only. What it basically needs to do is find "01AStart" and select from that row through to the row containing "01AEnd" and then if there are no numeric values in that range it needs to hide it. Then it needs to move on to 01B, then 01C, etc etc. That is just for sheet 01. There are 30 sheets in total, named 01 through to 30, and there varying codes in each sheet (i.e. 01 goes up to 01K and 30 only has 30A).

    Ideally I'd like one macro that can be used throughout the whole workbook (but would only actually run on the active sheet).

    I think a loop array would work but I'm not quite sure how to write it. And I'd also need it to be a variable array rather than static (i.e. not error when it doesn't find 30B or something like that).

    Hope that makes sense...

    Thanks
    Last edited by lealea1982; 08-26-2011 at 07:02 AM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Need help with loops and arrays in VBA Macro

    lealea1982,

    Some clarification please. The 01A, 01B, etc. Are these all in the same column? Or is 01A in column A, 01B in column B, etc.?

    ~tigeravatar

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Need help with loops and arrays in VBA Macro

    Hi lealea1982, It may be best for you and those that will help if you upload a mock workbook with mock data in it.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  4. #4
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    Sorry yes, they're all in Column A. I've attached an example of how it would look. The "Start" and "End" cells would actually have white font and be protected (so they're invisible) but it should give you an idea of what I mean. The first sheet shows how it would look before the macro, the second sheet shows how it would look after the macro.

    Many thanks!
    Attached Files Attached Files

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Need help with loops and arrays in VBA Macro

    lealea1982,

    Assuming the sheets are named "01", "02", etc (no quotation marks) and nothing else, and the 01AEnd etc is correct (in the sample workbook, you had 01FStartEnd instead of 01FEnd, I changed it when testing the macro to just 01FEnd and it worked) you should be able to use the following:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

    EDIT: Shortened code, updated code to work for more than single digit columns
    Last edited by tigeravatar; 08-24-2011 at 12:17 PM. Reason: Updated code

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    Assuming the above you could also use - not quite as elegant as above


    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-24-2011 at 12:19 PM. Reason: Fix code

  7. #7
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    That works perfectly, exactly what I'm after... only one thing though, at the moment it only runs the loop up to 01F, is it possible to get it to keep going until it runs out of codes? For example, in my 05 page it starts at 05A and runs right through the alphabet all the way to 05AG!

    Thanks again for the help, it's so appreciated!!

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    have a whirl with mine *should* work for any amount of data
    Last edited by Kyle123; 08-24-2011 at 12:19 PM. Reason: problem with code

  9. #9
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    Absolutely perfect!! Thank you so much!!

  10. #10
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    Quick addition to this one if that's ok...

    On a couple of my pages the tables only contain formulas, and no hard values. This macro doesn't seem to work on any tables that contain formulas only. Do you know how I can adjust it so that it'll pick up on that?

    Alternatively, could the macro be amended to only hide the cells where the SUM = 0?

    Many thanks again!!

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    Have a try with this: - haven't tested it though

    Edit Actually just tried the original code on tables with only formulas and it worked :S
    Could you re-post your workbook with the tables that don't work?

    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-25-2011 at 11:45 AM.

  12. #12
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    That one hides all of them unfortunately (both formulas and values).

    Is there a way to make it hide those where the SUM of the highlighted range is 0? That might make it work a bit better for me.

    Thanks again!

  13. #13
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    Here it is. I've highlighted the cell that has just a formula in it in yellow - that macro seems to hide that table though...
    Attached Files Attached Files

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    This works

    Probably a neater way of writing it though

    Please Login or Register  to view this content.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Need help with loops and arrays in VBA Macro

    Maybe:

    Please Login or Register  to view this content.
    Last edited by snb; 08-26-2011 at 03:37 AM.



  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    Hehehe brilliant, as I said - always a neater way
    Last edited by Kyle123; 08-25-2011 at 03:39 PM.

  17. #17
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    Quote Originally Posted by Kyle123 View Post
    This works

    Probably a neater way of writing it though

    Please Login or Register  to view this content.

    This one worked perfectly (the second one wouldn't hide anything, I think possibly because the totals have complex formulas in them (more complex than the example workbook I attached before).

    The only problem now is if it picks up a formula within the table it doesn't hide it, even if the result of the formula is zero (2 of my pages have tables that are filled with formulas - many of which may be 0). Is it better to do a macro that picks up whether the SUM of the tables is zero for those 2 pages?

    Thanks for all your genius help guys!

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    try:

    Please Login or Register  to view this content.

  19. #19
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Need help with loops and arrays in VBA Macro

    @Kyle

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    10-23-2007
    Posts
    80

    Re: Need help with loops and arrays in VBA Macro

    I can't get either to work unfortunately! When I use the first code it hides everything, when I insert the second code it doesn't hide anything!
    Might have to admit defeat with this one!


    Scrap that - it's working!! Hurrah!! All I did was close it all down and open it again in frustration.

    Thank you so much for your help, you've made my day!!
    Last edited by lealea1982; 08-26-2011 at 07:01 AM. Reason: It works now!!

  21. #21
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    @snb won't that won't that always return true? On error resume next will skip to the 'then' Part so we need an error catch

    It does reduce code though, I've amended based on your idea:


    Please Login or Register  to view this content.


    lealea1982, I have checked this and it works in the attached - I have a formula that results in 0 in one of the tables - the code hides this table
    Attached Files Attached Files

  22. #22
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Need help with loops and arrays in VBA Macro

    @Kyle

    Did you test the code ?

    Please Login or Register  to view this content.

  23. #23
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with loops and arrays in VBA Macro

    @snb

    Yes, try the following as per your original suggestion, in an If statement, the code fails so resumes next:


    Please Login or Register  to view this content.

  24. #24
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Need help with loops and arrays in VBA Macro

    @kyle,

    Use
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1