vba to copy, paste, run code, loop

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
165
I started a previous post but I think I over-complicated it, so I will try again with (hopefully) a better explanation of what I'm trying to achieve:

Sheet "Dates" contains dates from A2:A200 (the range can be less or more)

Step 1: Copy the first date from "Dates" sheet in cell A2
Step 2: Paste this date to cell A1 in sheet "Pair" (dates will always be pasted in cell A1)
Step 3: Run the below code
Step 4: Copy the next date (A3) in "Dates" sheet
Step 5: Repeat step 2
Step 6: Run the below code
Step 7: Copy the next date (A4) in "Dates" sheet
Step 8: Repeat step 2
Step 9: Run the below code

...repeat these steps until the last date in sheet "Dates" has been reached, then stop.


Code:
Sheets("Pair").Select
    Range("A4:Z80").Select
    Selection.Copy
    Sheets("Net").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    'Deletes rows in column O if cells = "0"
    Dim LR As Long, i1 As Long
    LR = Range("O" & Rows.Count).End(xlUp).Row
    For i1 = LR To 1 Step -1
    If Range("O" & i1).Value = 0 Then Rows(i1).Delete
    Next i1

    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Sheets("Total").Select
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveSheet.Paste

Thank you.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this
Code:
Sub RunInLoop()
    Dim cel As Range
    With Sheets("Dates")
        For Each cel In .Range("A2", .Range("A201").End(xlUp))
            Sheets("Pair").Range("A1") = cel
            Call [I][COLOR=#ff0000]YourMacroName[/COLOR][/I]
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,635
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top