macro takes too long

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
This macro is taking about 1 second to loop through just 1 iteration and it is a very simple macro. With about 700 interations it will take more than 11 minutes.

What is wrong?

Code:
Sub dates()


Dim thedate As Date


thedate = ActiveCell.Offset(-1).Value
Dim i As Integer
Application.ScreenUpdating = False




Do Until ActiveCell.Offset(0, 1) = ""
i = i + 1




    If ActiveCell.Offset(0, 2).Value <> "x" Then
    ActiveCell = thedate
    Else
    thedate = thedate + 1
    ActiveCell = thedate
    End If
ActiveCell.Offset(1).Select




Loop


Application.ScreenUpdating = True


End Sub
 
What it does is that it prints the same date in the e column as the cell above, unless there is an x in the adjacent g column. Here is a screen shot

Screenshot2014-09-28at43357PM_zpsa6499e26.png
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Based on that, perhaps

Code:
Sub kyle()
    Dim iRow As Long

    For iRow = 2 To Cells(Rows.Count, "G").End(xlUp).Row
        If Cells(iRow, "G") = "x" Then
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value + 1
        Else
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value
        End If
    Next iRow
End Sub

or

Code:
Sub kyle()
    Dim iRow        As Long

    For iRow = 2 To Cells(Rows.Count, "G").End(xlUp).Row
        Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value - (Cells(iRow, "G") = "x")
    Next iRow
End Sub
 
Last edited:
Upvote 0
Reckon those need to start in row 3.
 
Upvote 0
That formula worked with just one minor change but it is still taking me 85 seconds to do 50 iterations. It was taking 93 seconds so I tried eliminating the following formula on another worksheet which I would think would be irrelevant.
Code:
=SUMPRODUCT([time.xlsm]time!$I$1418:[time.xlsm]time!$I$39435,--([time.xlsm]time!$E$1418:[time.xlsm]time!$E$39435=$B4),--([time.xlsm]time!$G$1418:[time.xlsm]time!$G$39435=C$2))

That formula only appeared in about 50 cells. I then removed the following formula in about 30,000 cells:


=VLOOKUP(G35755,key!$A$1:key!$B$75,2,0)

But that only gained 4 seconds in time. Andrew, the legend, was talking about something firing events that might be slowing things down. Do you know what he means.
 
Upvote 0
It should run in less than the blink of an eye.

Try this:

Code:
Sub kyle()
    Dim iRow        As Long

    On Error GoTo Oops
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    For iRow = 3 To Cells(Rows.Count, "G").End(xlUp).Row
        If Cells(iRow, "G") = "x" Then
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value + 1
        Else
            Cells(iRow, "E").Value = Cells(iRow - 1, "E").Value
        End If
    Next iRow

Oops:
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks, that did it. 600 iterations in 1.9 seconds. Excel for PC is much faster than Excel for Mac, so with Excel for PC it probably would have done it in .5 seconds.
Just out of curiosity, how does this code compare time-wise? Note the red highlighted code... it assumes cell E2 contains the starting date, so if that assumption is wrong, then change the E2 to the cell address which does contain the date.
Code:
Sub Kyle2()
  Dim X As Long, Dte As Date, Cat As Variant, Dates As Variant
  Dte = [COLOR=#FF0000][B]Range("E2").Value[/B][/COLOR]
  Cat = Range("G3", Cells(Rows.Count, "G").End(xlUp))
  ReDim Dates(1 To UBound(Cat), 1 To 1)
  For X = 1 To UBound(Cat)
    If Cat(X, 1) = "x" Then Dte = Dte + 1
    Dates(X, 1) = Dte
  Next
  Range("E3").Resize(UBound(Dates)).Value = Dates
End Sub
 
Upvote 0
Nothing to do with speed, but why are you using this syntax?

[time.xlsm]time!$I$1418:[time.xlsm]time!$I$39435
key!$A$1:key!$B$75

It should be:

[time.xlsm]time!$I$1418:$I$39435
key!$A$1:$B$75
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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