+ Reply to Thread
Results 1 to 16 of 16

How to loop a recorded macro in a specified number of times?

  1. #1
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    How to loop a recorded macro in a specified number of times?

    I recorded a macro which inserts rows in the way I want.

    For example I want it to repeat 50 times.

    How to do that?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    What is the purpose of the macro? Where does it start?
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    There is a pattern consists of 10 rows. (From the row 1 to the row 10.)

    I want this pattern repeats 50 times. So It repeats to the row 500.

    I selected the second row (the row 2) then started recording the macro.

    The point is I create blank rows in these every patterns in a specified way.
    Last edited by zanshin777; 03-17-2019 at 06:23 PM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  5. #5
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    I uploaded the file.

    There is a software called Macro Recorder.

    You record a macro on Windows then you specify a number for example 50. Then It repeats the macro for 50 times.

    I thought It could be done on Excel and It is applicable for every macro. Not just for this case.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    Does anybody have any ideas?

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    OK, try this ... suggest you test it on a copy of the worksheet first:

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    Excel freezes when I run the macro.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    Comment out a couple of lines ...

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    Now it works. Thank you very much.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    You're welcome.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    Thanks for the rep.

  13. #13
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    May I ask the part

    "With Range("A" & (i - 1) * 15 + 2)"

    in the macro?

    Could you explain that part please?

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    OK, well, you start in row 1 and you have blocks of 10 rows including a header, eight (8) data records, and a blank row. You want to insert 5 blank rows in each block.

    Your original macro started in row 2, after the first header block. After you have inserted 5 blank rows into the first block, the header for the second block will be in row 16 and you need to process from row 17. That is, having inserted the blank rows, we need to move on 15 rows to get the next block.

    So, the way the loop works is this:

    Please Login or Register  to view this content.

    i = 1: i - 1 = 0; (i - 1) * 15 = 0; (i - 1) * 15 + 2 = 2; so the address becomes Range("A2")
    i = 2: i - 1 = 1; (i - 1) * 15 = 15; (i - 1) * 15 + 2 = 17; so the address becomes Range("A17")
    i = 3: i - 1 = 2; (i - 1) * 15 = 30; (i - 1) * 15 + 2 = 32; so the address becomes Range("A32")
    i = 4: i - 1 = 3; (i - 1) * 15 = 45; (i - 1) * 15 + 2 = 47; so the address becomes Range("A47")
    i = 4: i - 1 = 4; (i - 1) * 15 = 60; (i - 1) * 15 + 2 = 62; so the address becomes Range("A62")
    and so on.

    The addresses to be inserted are all offset from the first row of each block (after the previous blocks have been adjusted by inserting the blank rows.

    Hope that helps. You could step through the code using F8 and watch the rows being inserted into each block if that helps you to visualise the process.

  15. #15
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: How to loop a recorded macro in a specified number of times?

    Understood.

    Thank you very much for your detailed answer.

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,373

    Re: How to loop a recorded macro in a specified number of times?

    You're welcome. Thanks again for the rep.


    This is an alternative approach. It starts from the bottom up so you don't need to calculate the starting point for each block ... it's always 10 rows before the block being processed. Maybe easier to follow but not as aesthetically pleasing.

    Please Login or Register  to view this content.

+ 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. How To Loop A Recorded Macro
    By rcdavis28 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-29-2017, 06:15 AM
  2. creating loop for my recorded macro
    By Rickieee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2015, 12:10 PM
  3. [SOLVED] Macro to Loop X times X= the number of Cells with content from A1 to A10
    By excelcandy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2013, 05:03 PM
  4. I need a macro that will let me loop a section of the vba code x number of times
    By tuckejam in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2013, 02:44 PM
  5. Make recorded macro loop
    By cowannbell in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 01-11-2013, 09:53 AM
  6. [SOLVED] VBA loop to copy a worksheet a number of times, assign name each time based on loop number
    By TBG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 10:54 PM
  7. Create a loop to call macro a specified number of times
    By magito in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-09-2011, 09:18 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