+ Reply to Thread
Results 1 to 13 of 13

Ending a Loop

  1. #1
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Unhappy Ending a Loop

    Hello, I am working on a macro that takes a selection from a database (the selection is one or more entire rows) and creates and prints a report for each row selected. (It is currently set up to just create a new workbook of the report rather than print it for testing.)To use it, I must have the database (Database.xlsm) open and the sheet "DATABASE" selected. I manually select which row(s) I would like reports for and then run the macro. I am experiencing problems making the loop end. It does not create reports only for the selection, but keeps creating reports until my computer crashes basically. I've tried to use the Excel Help included with the program but I can't figure out what I'm doing wrong. Thank you if you can let me know!

    The macro is currently:

    Please Login or Register  to view this content.

    Thank you very much!
    Last edited by kirsty; 11-24-2010 at 09:38 PM.

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

    Re: Please help me stop looping!

    Be sure to read through the Forum Rules so you can use and follow them effectively.

    Specifically, you'll need to EDIT that post above and put code tags around that code you used. (Like shown in my signature)

    Also, fix the title to reflect the topic only, take out the personalization: "Ending a Loop"
    _________________
    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!)

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

    Re: Ending a Loop

    The use of "activecell" is where you're going awry. Once you've set a range, specifically go through those cells without "selecting" or "offsetting".

    Try this:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    Thank you JBeaucaire. I have revised my question as requested.

  5. #5
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    I tried your suggestion and somehow the new code did exactly what the old one did? I have included the entire example below:

    Please Login or Register  to view this content.
    Last edited by romperstomper; 11-24-2010 at 03:56 AM.

  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: Ending a Loop

    Assuming the data sheet is in pristine condition to start each time, copy that sheet BEFORE you make any changes to it and you won't have to "reset it" each time.

    Please Login or Register  to view this content.

    I'm doing all I can to stop all the selecting and such, so there's no reason the code should "run wild" unless the other macro (which I took out the call to) was doing more uncontrolled selecting and changing things substantively.


    If that doesn't work, post an actual work book so I can see what you're seeing.

  7. #7
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    Wow, thank you very much for cleaning it up like that. I have been trying to teach myself VBA for a couple of months using the macro recorder and google but I haven't yet learned how to use With. That helps a lot.

    Unfortunately that code returned this error:
    Object doesn't support this property or method (Error 438)

    I will upload the workbook. Thanks again!
    Attached Files Attached Files

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

    Re: Ending a Loop

    You're working much harder than you need to. Here's your sheet with the formulas already in the DATA SHEET template ready and watching row 43 for when you put something there. Then this shorter macro does the job.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    Very clever, thank you! I hate to ask, but did this work on your computer? The workbook you have attached is still somehow creating infinite workbooks from a two row selection.

  10. #10
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581

    Re: Ending a Loop

    Hi,

    This has to do with how you're chosing your range and looping.

    The line For Each Rw in RNG actually means that this is going to go through and run the subsequent code once for every cell selected. If you select 10 rows (the entire rows), it's going to generate (or attempt to generate) 10*16384 = 163,840 spreadsheets.

    The following changes result in only the first column of the selected data ending up being iterated through (instead of every cell).

    Please Login or Register  to view this content.
    S

  11. #11
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    Maistrye, are you married?

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

    Re: Ending a Loop

    My apologies. I did indeed only test my macro by selecting a few cells in column A each time.

  13. #13
    Registered User
    Join Date
    10-18-2010
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Ending a Loop

    Thank you very much for all the help you provided with simplifying my macro and helping me understand how to use VBA more efficiently!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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