+ Reply to Thread
Results 1 to 7 of 7

macro to cycle through randomly named worksheet in workbook

  1. #1
    Registered User
    Join Date
    01-18-2011
    Location
    chester, england
    MS-Off Ver
    Excel 2007
    Posts
    5

    macro to cycle through randomly named worksheet in workbook

    Hi,

    I'm trying to get a macro to cycle through all the sheets in a work book which have a value in cell Q2.

    For each cell it should perform a simple function involving entering some code and basic copying and pasting. It should then copy and paste some cells into another (already opened) workbook, 30aug.xlsx, before returning to the original workbook and cycling through to the next sheet.

    I wrote the following piece of code:

    Please Login or Register  to view this content.
    the first error I encounter is a next without for (applied to the for contained within the if code), however, even when i remove the if code and ask it to cycle through every worksheet in the workbook it just keeps looping through the same sheet over and over, never progressing to the next sheet.
    My worksheets have random names as oppose to sheet1, sheet2, sheet3 etc... is this the problem.

    I'm an novice, what am I doing wrong?

    Thanks for all and any help
    Ben

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: macro to cycle through randomly named worksheet in workbook

    This will do what you are looking for.

    Please Login or Register  to view this content.
    Your problems are caused because the if..end if can't have a next ws in it. You need to structure the statement like I did. It helps if you use indentation to deep track of your ifs and withs and fors. You also need the With ws or your code will always be operating on the active worksheet. It also is almost never necessary to Select cells. You can reference the values without doing so, and your program will run smoother and faster.

    If you have any problems or questions, please ask.
    Last edited by davegugg; 01-21-2011 at 05:12 PM. Reason: Correcting at JB's suggestion (Thanks JB!)
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    01-18-2011
    Location
    chester, england
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: macro to cycle through randomly named worksheet in workbook

    Thanks Dave,

    I tried running it and nothing happens, so I ran it through step by step and it runs through, the first sheet has nothing in Q2 so it correctly jumps to the endif, it then goes onto next ws and cycles back to the start but remains on the same worksheet and cycles through the code again.

    Any idea why it is not moving on to the next worksheet in the active workbook? The worksheets are named randomly, is this a problem?

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: macro to cycle through randomly named worksheet in workbook

    Well, it won't activate the worksheet it is working on, but it will cycle through all the worksheets. You can test this by typing "print ws.name" in the immediate window every time you loop to a new sheet.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to cycle through randomly named worksheet in workbook

    Correct this one line:

    Please Login or Register  to view this content.

    Dave, you might want to edit the original macro above so it's correct for anyone searching in the future.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro to cycle through randomly named worksheet in workbook

    Also, if you tell us what the sheetname is in the 30aug.xlsx file, then you could copy these values without having to "activate" back and forth, just like "selecting" isn't necessary, usually "activating" isn't necessary either.

  7. #7
    Registered User
    Join Date
    01-18-2011
    Location
    chester, england
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: macro to cycle through randomly named worksheet in workbook

    Hi Thanks again for all your help everyone.

    I think the problem was that it kept writing the copied data into the same 3 cells on the 30aug.xlsx workbook (A2, B2 and C2) instead of writing it into the next row each time. I plugged a bit of my old messy code in and its working.

    Final code was

    Please Login or Register  to view this content.
    Thanks both for the help getting the code right and for the advice in general on how to write these things more accurately.

+ 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