Optimizing VBA Code

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
I have written the following code to test the efficiency of 3 different user definded functions. Looking at the results, runs 2 through 10 are taking about 30% longer to run than the 1st run. Is there something that can be done to fix this?

Code:
Sub Macro1()

Dim dtstart As Date
Dim iptr As Long
Dim I As Integer

For I = 1 To 10
    dtstart = Now()
    For iptr = 1 To 1000000
       Sheets("Sheet1").Cells(1, 1).Value = iptr
    Next iptr
    Sheets("Sheet1").Cells(I, 13).Value = Now() - dtstart
Next I

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What do you mean "fix"?
The before and after of your code and this are identical, but I don't think this is the kind of answer you want.
Code:
Dim dtstart As Date
Dim dtduration As Date
Dim I As Integer
dtstart = Now()
    
For iptr = 1 To 1000000
   Sheets("Sheet1").Cells(1, 1).Value = iptr
Next iptr
    
dtDuration = Now()- dtStart

Sheets("Sheet1").Cells(I, 13).Value = dtDuration

For i = 1 to 9
    Wait dtDuration
Next i
 
Upvote 0

Forum statistics

Threads
1,216,462
Messages
6,130,781
Members
449,591
Latest member
sharmavishnu413

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