Loop/Repeat Macro

c0087

Board Regular
Joined
Jul 13, 2015
Messages
85
Office Version
  1. 365
Platform
  1. Windows
I'm trying to automatically run this so after it finishes copying Sheet "Corp" to a new sheet, it will change the R[-2] to R[-3], then copy/paste "Corp", change to R[-4], etc etc. The final run should be R[-1465].

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
  
    ActiveCell.FormulaR1C1 = "=ABS(CD_4[@N2]-R[-2]C[-8])"
    Range("L1468").Select
    Selection.AutoFill Destination:=Range("L2:L1468"), Type:=xlFillDefault
    Range("L2:L1468").Select
    Sheets("Corp").Select
    Cells.Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Speaking for myself only, I don't find it clear from your existing code or comments what it is that you want to happen. Can you give any further information, or perhaps examples?
 
Upvote 0
Speaking for myself only, I don't find it clear from your existing code or comments what it is that you want to happen. Can you give any further information, or perhaps examples?
 
Upvote 0
As @CephasOz said, it's not entirely clear what you want, but perhaps a loop that replaces the R[-2] in the first line is what you are seeking? If so you could do this with a For loop.

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
    Dim i as long
'
 
    For i = -2 to -1465 Step -1
         ActiveCell.FormulaR1C1 = "=ABS(CD_4[@N2]-R[i]C[-8])"  '<= Replace -2 with i in this line
         Range("L1468").Select
         Selection.AutoFill Destination:=Range("L2:L1468"), Type:=xlFillDefault
         Range("L2:L1468").Select
         Sheets("Corp").Select
         Cells.Select
         Selection.Copy
         Sheets.Add After:=ActiveSheet
         Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _
             xlNone, SkipBlanks:=False, Transpose:=False
    Next i
End Sub

As an aside, I gather you recorded this macro. As a result quite a bit of the code is redundant. For example, it is usually not necessary to use .Select in VBA code as the objects can be combined. So for example the lines:
Sheets("Corp").Select
Cells.Select
Selection.Copy
can be combined into one line:
Sheets("Corp").Cells.Copy

Regards

Murray
 
Upvote 0
Speaking for myself only, I don't find it clear from your existing code or comments what it is that you want to happen. Can you give any further information, or perhaps examples?

As @CephasOz said, it's not entirely clear what you want, but perhaps a loop that replaces the R[-2] in the first line is what you are seeking? If so you could do this with a For loop.

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
    Dim i as long
'
 
    For i = -2 to -1465 Step -1
         ActiveCell.FormulaR1C1 = "=ABS(CD_4[@N2]-R[i]C[-8])"  '<= Replace -2 with i in this line
         Range("L1468").Select
         Selection.AutoFill Destination:=Range("L2:L1468"), Type:=xlFillDefault
         Range("L2:L1468").Select
         Sheets("Corp").Select
         Cells.Select
         Selection.Copy
         Sheets.Add After:=ActiveSheet
         Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _
             xlNone, SkipBlanks:=False, Transpose:=False
    Next i
End Sub

As an aside, I gather you recorded this macro. As a result quite a bit of the code is redundant. For example, it is usually not necessary to use .Select in VBA code as the objects can be combined. So for example the lines:
Sheets("Corp").Select
Cells.Select
Selection.Copy
can be combined into one line:
Sheets("Corp").Cells.Copy

Regards

Murray
Yes I did record it, and yes this is exactly what I want done, however I'm getting a "run-time error 1004" on the ActiveCell line
 
Upvote 0
It seems to be an error with FormulaR1C1 ... so if I insert the actual formula

=ABS(CD_4[@N2]-D1467

how can apply your solution? in this case it would need to go from =ABS(CD_4[@N2]-D1467 to =ABS(CD_4[@N2]-D1466 to =ABS(CD_4[@N2]-D1465 , etc all the way to =ABS(CD_4[@N2]-D2
 
Upvote 0
My apologies I missed this. 1004 errors can be hard to track down. It should work if you do: (remeber to include the dim statement)

dim i as long
For i=2 to 1467
ActiveCell.Formula = "=ABS(CD_4[@N2]-D" & i
 
Upvote 0
Try
VBA Code:
Sub Macro1()
Dim T&
    For T = 2 To 1465
    ActiveCell.FormulaR1C1 = "=ABS(CD_4[@N2]-R[-" & T & "]C[-8])"
    Range("L1468").Select
    Selection.AutoFill Destination:=Range("L2:L1468"), Type:=xlFillDefault
    Range("L2:L1468").Select
    Sheets("Corp").Select
    Cells.Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Next T
End Sub
 
Upvote 0
My apologies I missed this. 1004 errors can be hard to track down. It should work if you do: (remeber to include the dim statement)

dim i as long
For i=2 to 1467
ActiveCell.Formula = "=ABS(CD_4[@N2]-D" & i
Unfortunately I'm still getting the same 1004 error even with this change
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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