+ Reply to Thread
Results 1 to 6 of 6

VBA to repeat macro x times

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    VBA to repeat macro x times

    Hi Everyone,

    I have a macro as below

    Sub Macro2()
    '
    For i = Range("A" & Rows.Count).End(3)(1).Row To 2 Step -1
        If Range("D" & i) = "Task 1" Then
            Range("D" & i + 1).EntireRow.Insert
            Rows(i + 1).Value = Rows(i).Value
            Cells(i + 1, 4).Value = "Task 1"
            x = i + 2
            Cells(i + 1, 5).Value = "=R[-1]C+1"
            Cells(i + 1, 6).Value = "=R[-1]C+1"
        End If
    Next i
    '
    End Sub
    Its basically looking for "Task 1" and then inserts a line underneath, copies the line from above and adds a day to start and finish.

    I want to be able to repeat this 21 times, so that I end up with 21 "Task 1"s with a start date over 21 days

    I have tried a couple of loops but I cant get this to work.

    Does anyone have any ideas?

    Thanks
    Alex

  2. #2
    Registered User
    Join Date
    02-09-2014
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: VBA to repeat macro x times

    Try this

    Sub Macro2()
    '
    For I = Range("A" & Rows.Count).End(3)(1).Row To 2 Step -1
        If Range("D" & I) = "Task 1" Then
            For j = I To I + 20
                Range("D" & j + 1).EntireRow.Insert
                Rows(j + 1).Value = Rows(I).Value
                Cells(j + 1, 4).Value = "Task 1"
                x = I + 2
                Cells(j + 1, 5).Value = "=R[-1]C+1"
                Cells(j + 1, 6).Value = "=R[-1]C+1"
            Next
        End If
    Next I
    '
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to repeat macro x times

    Maybe:

    Sub cossie2kaazz()
    Dim i As Long
    Dim z As Long
    z = 21
    Do Until z = 0
    For i = Range("A" & Rows.Count).End(3)(1).Row To 2 Step -1
        If Range("D" & i) = "Task 1" Then
            Range("D" & i + 1).EntireRow.Insert
            Rows(i + 1).Value = Rows(i).Value
            Cells(i + 1, 4).Value = "Task 1"
            x = i + 2
            Cells(i + 1, 5).Value = "=R[-1]C+1"
            Cells(i + 1, 6).Value = "=R[-1]C+1"
        End If
    Next i
    z = z - 1
    Loop
    End Sub

  4. #4
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: VBA to repeat macro x times

    thanks for the super quick responses!

    ihmha - Im trying this out at the minute so will let you know
    JOHN H - I get an error with this - Its a compile error (Next withoutFor)

    Ill try to build on these.

    Thanks again
    Alex

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to repeat macro x times

    Note code in Post#3 was edited. Current version should not produce an error.

  6. #6
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: VBA to repeat macro x times

    Apologies for the delay in getting back to you both, both of these work a treat.

    Ive actually gone with ihmha's suggestion as it seems to fit my requirements better.

    Thank you both so much for your help
    Alex

+ 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. Replies: 4
    Last Post: 03-08-2014, 08:01 AM
  2. [SOLVED] Macro - Repeat macro a specific number of times
    By uvinduds in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2013, 02:42 PM
  3. Need macro help with transposing columns to rows and repeat a row value multiple times
    By InnovativeAJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 11:53 AM
  4. How to repeat a macro x number of times based on a value in a cell
    By gypsyhotelier in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-19-2011, 05:11 PM
  5. How to repeat a macro many times
    By cbo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2010, 05:41 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