Can this VBA run faster?

phil133

Active Member
Joined
May 5, 2015
Messages
257
Office Version
  1. 365
Platform
  1. Windows
Hi. I have written the following VBA
Code:
Sub calculate_macro1()'
' test Macro
'


'
    Sheets("P_L 2,ATM").Select
    Range("G6:G400").Select
    Range("G6:G400").Value = 100
    For i = 6 To 500 Step 2
    Range("X" & i).Copy
    Range("G" & i).PasteSpecial (xlPasteValues)
    Range("G" & i).Copy
    Range("G" & i + 1).PasteSpecial (xlPasteValues)
    Range("X" & i).Activate
    ActiveSheet.Calculate
    Next i
End Sub

How can I change it so that it runs faster?

Thanks for any help!
 
Is it normal that it takes 5 minutes 10 seconds?

Seems very high even for the code you posted as the code below which is yours with a couple of adaptions running on a sheet with only constants ran in 360.51 milliseconds on my laptop.

Code:
Sub calculate_macro1()
Dim i As Long
    Application.ScreenUpdating = False
    With Sheets("P_L 2,ATM")
        .Range("G4:G5000").Value = .Range("G2").Value
        For i = 6 To 4000 Step 2
            .Range("G" & i).Resize(2).Value = .Range("AA" & i).Value
        Next i
    End With
    ActiveSheet.Calculate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
With calculation set to manual it ran in 2.43 seconds on my laptop, ran on its own.

Code:
Sub calculate_macro1xxx()
    Dim i As Long
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    With Sheets("P_L 2,ATM")
        .Range("G4:G5000").Value = .Range("G2").Value
        For i = 6 To 4000 Step 2
            .Range("G" & i).Resize(2).Value = .Range("AA" & i).Value
        Next i
    End With
    
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    ActiveSheet.Calculate
End Sub
 
Upvote 0
Thank you! This took it to 6 seconds. I'm very grateful to you!
 
Upvote 0
Unfortunately, the results are not how I wanted them. It needs to automatically calculate because ie. G6 is determined by AA6 which is determined by G4. I guess I will stick with the 5 min macro! Better safe than sorry!
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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