+ Reply to Thread
Results 1 to 11 of 11

Do Loop with variable range

  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Do Loop with variable range

    The macro below works between 2 different worksheets. This is adding alternate parts to the first spread sheet. On the first spreadsheet, it adds 13 blank rows, then takes the PN from column B above those added rows, copies it into a different spreadsheet that spits out a set of alternate PNs and some associated data. It then first copy,past values of the alternate PNs back into the original spreadsheet in column B below the original PN, then goes back over and copies the associated data of those alternates (columns D to AC) back into the first spreadsheet. Currently it works, but runs for a long time. The way I read this is that it basically will go through 3,150 Lines (endLocation = 225 * 13 +225. I want it to stop after it reaches a blank cell in Column B. I inherited this macro and have tried a few things that don't work. I think the gotcha is the fact that it adds lines each time, so telling it when it reaches the end is tricky. Any help or ideas is appreciated,

    Please Login or Register  to view this content.

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

    Re: Do Loop with variable range

    Does column B contain contiguous data or is the dat in that column separated by a specific no of rows?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with variable range

    Column B is contiguous data before the macro runs. The macro adds 13 blank rows, then goes to the other spreadsheet copies over some alternate PNs, then goes back over and copies over related data. After this macro processes another macro goes through and deletes blank rows.

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

    Re: Do Loop with variable range

    Adds 13 blank rows where?

  5. #5
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with variable range

    I am adding a couple of attachments here. I'm hoping this will clarify things Workbook_A contains the macros. The 13 lines are added here, the data copied over, then it drops down, adds 13 lines, copies the data etc. Currently it is repeating the process 225 times I believe. In this particular data set I'd want it to end after 94 times, but that is the variable.
    Attached Files Attached Files

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

    Re: Do Loop with variable range

    So you copy the Prime DPN from column B of worksheet 'Sheet2' to B2 of worksheet 'Family Tool'.

    You then copy the data produced in columns D:AF on Family Tool to column C of worksheet 'Sheet2' starting in the same row as the Prime DPN that had been copied.

    Couple of questions.

    Is it always 13 rows you insert? What about when only 4 rows of data are produced by 'Family Tool'?

    Do you copy values only?
    Last edited by Norie; 08-08-2013 at 11:47 AM.

  7. #7
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with variable range

    "So you copy the Prime DPN from column B of worksheet 'Sheet2' to B2 of worksheet 'Family Tool'." - Correct Cell B2 to Cell B2

    "You then copy the data produced in columns D:AF family tool to column C of worksheet 'Sheet2' starting in the same row as the Prime DPN that had been copied." Almost, Data produced is copied in 2 sets. First the data is copied from C2:C14 to column B of worksheet 'Sheet2' below the Prime DPN (in this case starting in B3. Then it goes back and copies Range D2:AF14 to starting Cell D3.

    Yes, 13 rows are always inserted. Prime DPNs have differing numbers of alternates. Some have none and some have up to 13. In this case only 4 rows of data would actually be copied (yes it is copy values only). After this macro completed another one runs and deletes all the blank rows.

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

    Re: Do Loop with variable range

    Sorry I'm a little confused.

    Can you post an after for Workbook A?

    It only need to be for one of the PNs.

    By the way, wouldn't it be easier to insert the required no of rows instead of 13 every time?

  9. #9
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with variable range

    Hotlist Template V3_0_SEND.xlsm

    Here is a final version. I deleted a bunch of the rows. The yellow highlighted rows are the Prime PNs and the non-highlighted rows are the alternates that were copied in. All of the blank rows have been deleted.

    I inherited this macro. For me, I don't think it would be easier to just add the qty of rows needed. That would require some coding that is beyond my abilities. 13 Rows may be too many to add, but I have not evaluated the # of alternates per prime to see if it is way to many. Besides, anything "extra" is deleted.

    Thank you for your interest in helping on this!!!

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

    I won't be able to have a look at the new file until later but hopefully it'll clear things up.

  11. #11
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with variable range

    Thanks, hope it does! I know I've done a lousy job of describing it. I'm new to macros, only been playing with them for about a month after inheriting a few. I've managed to solve a lot of the variable range type things, but this one has me stumped.

+ 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. Do Loop with Variable Range
    By Austex_egger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2013, 03:52 PM
  2. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  3. Referencing variable Range in a loop
    By Samirkc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2006, 07:55 AM
  4. [SOLVED] How to reference variable range in a loop
    By Samirkc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2006, 11:15 AM
  5. Setting a range within a loop variable for copy/paste
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2005, 12:10 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