How to make Macro quicker and shorter

SaraB802

New Member
Joined
Feb 7, 2013
Messages
34
So here is one that no doubt those VBA gurus amoung you can solve quickly. My code does a simple copy and paste funtion for a formula then overwrites it with a copy paste values. Below is the sample:
Code:
Sub Update_Actuals()
'
' Update_Actuals Macro
' Updates Actuals on Resources Data worksheet
'


'
    Range("R7").Select
    Selection.Copy
    Range("S7:KF7").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("R10").Select
    Selection.Copy
    Range("S10:KF10").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("R13").Select
    Selection.Copy
    Range("S13:KF13").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
So the above covers 3 rows, I need to do it for over 750 rows and really don't want to have to repeat this for all of them. So far I have got to row58 and it is already visually slow.

It needs to work of every 3rd row.

All help much appreciated.
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

On the assumption that you are doing every third row for a set number of rows, something like this:

Code:
Sub Update_Actuals()
'
' Update_Actuals Macro
' Updates Actuals on Resources Data worksheet
'


'

Dim lRow as Long

Application.screenupdating = False
For lRow = 7 to 757 Step 3

    With Range(Cells(lRow, "S"), Cells(lRow, "KF"))
        .FormulaR1C1 = Cells(lRow, "R").FormulaR1C1
        .Value2 = .Value2
    End With

Next lRow
Application.screenupdating = True
End Sub
 
Last edited:
Upvote 0
Code:
Sub Update_Actuals()
'
' Update_Actuals Macro
' Updates Actuals on Resources Data worksheet
'


'
Application.ScreenUpdating = False
For i = 7 To 2257 Step 3

Selector = "R" & i
Paster = "S" & i & "KF" & i
    Range(Selector).Copy
    Range(Paster).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
Next
   

Application.ScreenUpdating = True


End Sub
 
Upvote 0
I think Rory's code might be a bit more efficient than mine,

but be sure to put the following lines before and after the loop code for faster results:
Code:
application.screenupdating=false

and
Code:
application.screenupdating=true
 
Upvote 0
Hi,

On the assumption that you are doing every third row for a set number of rows, something like this:

Code:
Sub Update_Actuals()
'
' Update_Actuals Macro
' Updates Actuals on Resources Data worksheet
'


'

Dim lRow as Long

Application.screenupdating = False
For lRow = 7 to 757 Step 3

    With Range(Cells(lRow, "S"), Cells(lRow, "KF")
        .FormulaR1C1 = Cells(lRow, "R").FormulaR1C1
        .Value2 = .Value2
    End With

Next lRow
Application.screenupdating = True
End Sub

Hi Rory

Tanks for this I am getting a syntax error on the row

With Range(Cells(lRow, "S"), Cells(lRow, "KF")

Any idea why?
 
Upvote 0
Apologies - I missed a closing parenthesis. I have adjusted the code above.
 
Upvote 0
Hi Rory,

That fixed it great, now it is coming up with application defined or object defined error on the above phrase.

.Value2 = .Value2

Thanks
 
Upvote 0
Do you have any merged cells?
 
Upvote 0
Can you upload a file for review somewhere (eg OneDrive / Dropbox)? I'm struggling to think of what else might be the problem.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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