+ Reply to Thread
Results 1 to 3 of 3

Help with Recursive VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    10-26-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2008
    Posts
    3

    Help with Recursive VBA

    I have included below a macro that I am running and want to continue to run, but figure there is a better way to do it. As you can see, all I am doing is copying the cell A1 and pasting its value in column AO starting at row 2 and working my way down, pasting the value in each row. I want to print this about 100 times. Is there a way to do this without typing in every row? (Note: The value of A1 keeps changing due to random numbers, otherwise I would just print Range("AO2:AO102"). Thanks!

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    ' Keyboard Shortcut: Ctrl+p
    '
        Range("A1").Select
        Selection.Copy
        Range("AO2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
        Selection.Copy
        Range("AO3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
        Selection.Copy
        Range("AO4").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO5").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO6").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO8").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO9").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
        Selection.Copy
        Range("AO10").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO11").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO12").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO13").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("A1").Select
        Selection.Copy
        Range("AO14").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    End Sub

  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: Help with Recursive VBA

    Try this:

    Sub CopyXTimes()
    Dim X As Long, i As Long
    
    X = Application.InputBox("Copy A1 how many times?", "Repeat", 100, Type:=1)
    If X = 0 Then Exit Sub
    
        For i = 1 To X
            Range("AO1").Offset(i).Value = Range("A1").Value
        Next i
    
    End Sub
    _________________
    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
    Registered User
    Join Date
    10-26-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2008
    Posts
    3

    Re: Help with Recursive VBA

    This is perfect. Exactly what I was looking for. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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