+ Reply to Thread
Results 1 to 14 of 14

How to Exit Do Loop and go to other worksheets ?

  1. #1
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Question How to Exit Do Loop and go to other worksheets ?

    Hello,

    I'm writing a macro to post a monthly income journal. The main macro uses vlookup to search for an account number that corresponds to a code, and then inputs the debit or credit based on the day of the month. I need the macro to look through at least 6 worksheets, but I'm having trouble getting the macro to loop through more than the first worksheet ("HOTEL") after it finds a blank cell. I'm using a "Do Until" loop and trying to "Exit Do" and have the macro go on to the next worksheet ("WKST"), then the next worksheet, etc., but I can't make it happen. When it finds a blank cell and exits the loop, it moves down to the DEFINELOCATION macro, but I don't want that yet if it hasn't found the correct account number on the first worksheet.

    Thank you for any ideas on this! Here's the code I have:

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi Jason15,

    It would be better if you include the actual workbook or a workbook with some dummy data that I can work on.

    Cheers
    Remember you are unique, like everyone else

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason15,

    instead of looping through the cells for each sheet you could use a CountIf on each sheet and then decide what to do.

    Please Login or Register  to view this content.
    I asssumed WORK2 to be a nmaed range which holds the references.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Re: How to Exit Do Loop and go to other worksheets ?

    Hello noboffinme,

    I've attached a sample file. Thank you for you offer to look at this issue for me! (The "DAILY" worksheet contains the new data that is imported each day from a .txt file.)

    Hello HaHoBe,

    I'm still new to vba and the code you've added I'm not familar with, but I will test it and let you know. Thank you!
    Attached Files Attached Files
    Last edited by Jason15; 09-14-2014 at 04:35 PM.

  5. #5
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: How to Exit Do Loop and go to other worksheets ?

    Test HaHoBes code first & and come back to us.

    Cheers

  6. #6
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Re: How to Exit Do Loop and go to other worksheets ?

    HaHoBe,

    I've added your code to the FINDACCT macro, and I've tried to adapt it to what I'm trying to do, but have failed.

    I would like the code to look at the 6 worksheets ("HOTEL", "WKST"..."F&B") and find the matching account number from the DAILY worksheet. If the first account number isn't found, I want it to look at all the others. After each account number is found, the other sub macros DEFINELOCATION, PASTEVALUE OR ADDVALUE should post the corresponding debit or (credit). This worked for me on the "HOTEL" worksheet, but I couldn't get it to look through the other 5 worksheets. Perhaps these other macros are now not needed based on your code?

    Any other information you could provide would be very helpful to me. Thank you.
    Attached Files Attached Files

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason15,

    I assumed toi hold Rane("work2"9 what had to be searched for in each sheet. What you have attached is a sample of a workbook that need rewriting from the scratch: lots of names, lots of procedures which all work with ActiveCell instesad of using refernces to pass the information between the routines.

    Before running any loops you should make sure that the value will be available:
    Please Login or Register  to view this content.
    BTW: I didnīt find code in any of the UserForms.

    Ciao,
    Holger

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason15,

    asd I am still having trouble to figure out what should be copied/added from where to where could you please do me a favour and write down in plain words what the macro is supposed to do.

    This may prove to be the basic for finding the ACCT:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Re: How to Exit Do Loop and go to other worksheets ?

    Hello HaHoBe,

    The macro is supposed to copy each debit and (credit) from the DAILY tab into one of the the 7 sheets you've listed in your array. The DAILY tab is new data that is imported each day from a .txt file, so each day there are new debits and (credits). If it's the 1st day of the month, it should copy all the debits and (credits) into the 1st row of the correct sheet. If it's the 2nd day, copy into the 2nd row, etc. The CHARGE CODE tab is a table of all the existing codes and Acct numbers.

    For example, on 8/1/14 the first amount on the DAILY tab is an ($89.15) credit under code 14. This amount should be copied as ($89.15) to the HOTEL tab, cell M13, because the Acct number in column C of the CHARGE CODES tab matches cell M13 on the HOTEL tab. The sixth amount on the DAILY tab is a ($1,176.31) credit under code 113. This amount should be copied as ($1,176.31) to the WKST tab, cell F13. Code 903 on the DAILY tab is a $1,021.53 debit that should be copied as $1,021.53 to the ADJT tab, cell M13. Etc., Etc.

    Each debit and (credit) has a unique Acct number that is matched to the same unique Acct number in a column on one of the 7 sheets.

    I can try to clarify further if you need it. Thank you again for trying to solve this for me!

    Regards,
    Jason

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason,

    maybe the explanations you give should be fitting in with the attachment I have downloaded:
    For example, on 8/1/14 the first amount on the DAILY tab is an ($89.15) credit under code 14
    Both Charge Codes and Daily start with 15, and the code in Hotel should be found in Row 12 in the sample not 13 (and doesnīt fit the number for code 15).

    I believe what you have attached is quite an old concept but I still wonder why there are 7 months in the year which have 31 days while your lookup table only goes for 30 days (and the sheets go for 35 days for the month of August which is well into the next month September). The conecpt you had would not allow any data transfer for 31. August 2014.

    I may have gotten it wrong regarding to put in Credits and Debits (you may need to correct this part of teh code), and I put in the comment "done" in COlumn F in Daily when a match was found in any sheet and the transaction could be made.

    There is an additional question: will each code only appear in one sheet or is it possible that it would appear in more than one sheet? In the latter case you would have to remove the Exit For in order to keep the second loop going through the remaining sheets as well.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Re: How to Exit Do Loop and go to other worksheets ?

    Hello HaHoBe,

    Wow, except for one 'run-time error: type mismatch', you got it! This is simply fabulous!

    The debits and credits were reversed as you suspected, but I just switched the "+" and "-" and this was fixed. To answer your question, each code will only appear on one sheet.

    It does post all the debits and credits, but afterwards it gets stuck with the 'run-time error' type mismatch' here:
    Sh.Cells(lngRowNr, var).Value = Sh.Cells(lngRowNr, var).Value + wsDaily.Range("D" & lngCtr).Value
    Not sure what needs to be fixed, though, since all the amounts post correctly on the correct sheet.

    I do have one request. Is there a way to have the debit and credit amounts in columns D & E on the DAILY tab to not turn to zero, and instead leave the amounts there?

    Thank you,
    Jason

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason,

    Is there a way to have the debit and credit amounts in columns D & E on the DAILY tab to not turn to zero, and instead leave the amounts there?
    Delete or comment the code lines
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    It does post all the debits and credits, but afterwards it gets stuck with the 'run-time error' type mismatch' here:
    What do you mean with afterwards? If the credits and debits have been transferred is thae error showing up within the same run of the macro?

    Ciao,
    Holger

  13. #13
    Registered User
    Join Date
    09-12-2014
    Location
    New York
    MS-Off Ver
    2010
    Posts
    16

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi HaHoBe,

    Yes, the credits and debits have been transferred and the error shows up within the same run of the macro.

    Thanks,
    Jason

  14. #14
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to Exit Do Loop and go to other worksheets ?

    Hi, Jason,

    from what I used for code you would have to check the contents of the cell where the amount should be added (could be text or an error) if the values in Daily are only numeric for column D and E except for the headers. No error occurred when I ran the macro on the attachemnt for my side.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Exit For Loop on Error?
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2014, 01:49 PM
  2. [SOLVED] For Next Loop Does Not Exit
    By goss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2014, 04:52 PM
  3. ESCAPE ket to exit loop??
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2013, 07:49 PM
  4. Exit loop
    By ashmott in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2011, 05:41 AM
  5. [SOLVED] exit for next loop
    By David Henderson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2006, 02:45 PM

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