+ Reply to Thread
Results 1 to 10 of 10

Almost 6,000 lines ... I need help to loop or optimize

  1. #1
    Registered User
    Join Date
    02-09-2015
    Location
    Gyeongju, Korea
    MS-Off Ver
    2003 (until the end of time)
    Posts
    9

    Question Almost 6,000 lines ... I need help to loop or optimize

    I've been dragging this bit of code for years and always meant to tighten it up, but never got to it or could sort out how.

    This is for a gradebook, the code copies each students' data from the other sheets to a "grade report" that can be printed out. Mostly, it's just 30 macros which increment by 1, but now I need to expand that to 40 students and don't want to see this file get fatter with this stuff.

    Each macro is executed by a button ... the full workbook is attached (with identifying stuff scrubbed but zipped because all those lines ... )

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,932

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Replace all your code in Module 2 with the code in the second code block, where your last macro uses an argument and a variable i. The last macro is called by the same Subs you had, but they just pass the parameter you need to the new version. Creating the other 30 is left as an exercise for you I hope you see the pattern. I'm not sure what you workflow is but you could also do all 40 at once using a macro like this - if you print or save the report, that could be in the loop, too.

    Please Login or Register  to view this content.


    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 03-07-2015 at 09:47 AM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Another thing you can do is change code like this,
    Please Login or Register  to view this content.
    to this.
    Please Login or Register  to view this content.
    That's 3 lines to 1 line and if applied throughout will make a big difference to the length of the code.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    02-09-2015
    Location
    Gyeongju, Korea
    MS-Off Ver
    2003 (until the end of time)
    Posts
    9

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Quote Originally Posted by Bernie Deitrick View Post
    Replace all your code in Module 2 with the code in the second code block, where your last macro uses an argument and a variable i. The last macro is called by the same Subs you had, but they just pass the parameter you need to the new version. Creating the other 30 is left as an exercise for you I hope you see the pattern. I'm not sure what you workflow is but you could also do all 40 at once using a macro like this - if you print or save the report, that could be in the loop, too.
    Please Login or Register  to view this content.
    Bernie Deitrick,

    Your solution works very well, except for the snip above. Even futzing with the math a bit, I can't get the two rows of Homework to land correctly. The dummy scores count up from 1.00 and each button number should correspond with the value of first column of homework, e.g. student 04 would get 4.00, 4.00; student 12 would get 12.00, 12.00; etc. That explanation is probably not as clear as it could be. You can see this though if you play around with the buttons a little in the first workbook and compare to this one (with your suggestions).

    Attachment 381620
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-09-2015
    Location
    Gyeongju, Korea
    MS-Off Ver
    2003 (until the end of time)
    Posts
    9

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Norie,

    Thanks for the tip! (There are a lot of other places to tighten things in this workbook )

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,932

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Ooops. Sorry about that. Too little coffee in my blood when I replied:

    Your original was

    Worksheets("Homework").Range("F5:U6").Copy

    Change this

    Worksheets("Homework").Range("F" & I * 4 - 1 & ":U" & I * 4).Copy

    to this

    Worksheets("Homework").Range("F" & I * 2 + 3 & ":U" & I * 2 + 4).Copy

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Checkout your example and the YouTube video on how to make it work, I didn't finish it the code for you, but you will easily see what you need to do to finish it.


    Before.jpg

    After.jpg


    Right click the sheet tab to view the code.



    Please Login or Register  to view this content.
    -I wanted to use find instead of a loop, but had troubles finding the target.value in the sheets.



    Here's you video here
    https://www.youtube.com/watch?v=Am9_...ature=youtu.be
    Attached Files Attached Files
    Last edited by davesexcel; 03-07-2015 at 12:30 PM.

  8. #8
    Registered User
    Join Date
    02-09-2015
    Location
    Gyeongju, Korea
    MS-Off Ver
    2003 (until the end of time)
    Posts
    9

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Quote Originally Posted by davesexcel View Post
    Checkout your example and the YouTube video on how to make it work, I didn't finish it the code for you, but you will easily see what you need to do to finish it.


    Attachment 381626

    Attachment 381627
    davesexcel,

    Thanks for the video. I really like this idea as 40 students would be better accessed like this, but I can't figure out what that thing is or how to make it from scratch . I mean, it's not a combo box or a list box, is it?

  9. #9
    Registered User
    Join Date
    02-09-2015
    Location
    Gyeongju, Korea
    MS-Off Ver
    2003 (until the end of time)
    Posts
    9

    Re: Almost 6,000 lines ... I need help to loop or optimize

    Quote Originally Posted by Bernie Deitrick View Post
    Ooops. Sorry about that. Too little coffee in my blood when I replied:

    Your original was

    Worksheets("Homework").Range("F5:U6").Copy

    Change this

    Worksheets("Homework").Range("F" & I * 4 - 1 & ":U" & I * 4).Copy

    to this

    Worksheets("Homework").Range("F" & I * 2 + 3 & ":U" & I * 2 + 4).Copy
    Bernie Deitrick,

    This works perfectly. Don't blame the coffee though; the coffee is innocent

  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: Almost 6,000 lines ... I need help to loop or optimize

    It's a data validation list,

    See here how to do that

    https://www.youtube.com/watch?v=sTA9...ature=youtu.be
    Last edited by davesexcel; 03-08-2015 at 09:46 AM.

+ 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. VBA For loop, optimize speed and performance
    By britzer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-04-2014, 08:56 AM
  2. Is there any way to optimize this FOR loop?
    By lxsscott in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-23-2013, 03:50 PM
  3. Optimize for loop in order to win time
    By DamienR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 11:39 PM
  4. Optimize copy/paste loop
    By aldsv in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2005, 06:07 AM
  5. [SOLVED] Optimize VBA Excel 2003 NextFor loop
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2005, 09:06 AM

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