Tips on how to speed up a macro

Status
Not open for further replies.

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I know there are a lot of hardware variables on how fast it will run. It takes me approximately 1 min for 1,000 rows and there are a little over 10,000 rows.

Is there anything I can do to speed it up?

Thank you.

VBA Code:
Sub Compare_Dates()
    Dim rng As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rng4 As Range
    Dim LastRow As Long
    Dim i As Long
    Dim x As Long
    Dim sq, sp, Start, Last As Variant
    Dim sh As Worksheet
    
    Start = InputBox("Starting Row?")
    Last = InputBox("Ending Row? (Last Row = 0")
    
    Dim StartTime   As Double
    Dim MinutesElapsed As String
    StartTime = Timer
    
    For Each sh In ActiveWorkbook.Sheets
        If sh.Name Like "KPI Data*" Then sq = sh.Name
        If sh.Name Like "CR*" Then sp = sh.Name
    Next
    
    Sheets(sp).Select
    
    Range("A1:B" & Range("A" & Rows.Count).End(xlUp).Row).Copy
    
    Sheets(sq).Select
    
    Range("AL1").PasteSpecial
    
    LastRow = Cells(Rows.Count, "X").End(xlUp).Row
    
    If Last = 0 Then
        Last = LastRow
    End If
    
    Set rng2 = Range("AL2:AL" & Range("AL" & Rows.Count).End(xlUp).Row)
    Set rng3 = Range("AM2:AM" & Range("AL" & Rows.Count).End(xlUp).Row)
   
    For x = Start To Last
            
        If Range("W" & x) > 10 And Range("X" & x) > 1 Then
        
            For i = 1 To Range("X" & x)
                Columns("Z:Z").NumberFormat = "m/d/yyyy"
                Range("Z" & i).FormulaArray = "=INDEX(" & rng3.Address & ", SMALL(IF((" & Range("G" & x).Address & "=" & rng2.Address & "), MATCH(ROW(" & rng2.Address & "), ROW(" & rng2.Address & ")), """"),ROWS($A$1:A" & i & ")))"
                Range("AA" & i) = Range("Z" & i) - Range("N" & x)
                
            Next i
            Set rng4 = Range("AA1:AA" & Range("AA" & Rows.Count).End(xlUp).Row)
            
            Range("Y" & x).FormulaArray = "=INDEX(" & rng4.Address & ",MATCH(MIN(ABS(" & rng4.Address & "-0)),ABS(" & rng4.Address & "-0),0))"
        End If
        
        Range("Y" & x).Copy
        Range("Y" & x).PasteSpecial Paste:=xlPasteValues
        Columns("Z:AA").Clear
        
    Next x
    
    MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
    MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
    
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I added the below which cut the time down to around 30 seconds. Much better. If anyone else has some tips, please share. Thank you.

VBA Code:
    With Application
    .ScreenUpdating = False
    .DisplayStatusBar = False
    .EnableEvents = False
    End With
 
Upvote 0
I added the below which cut the time down to around 30 seconds. Much better. If anyone else has some tips, please share. Thank you.

VBA Code:
    With Application
    .ScreenUpdating = False
    .DisplayStatusBar = False
    .EnableEvents = False
    End With
How do I add this speed up code to this one (I'm new to VBA, sorry ?)

VBA Code:
Private Sub Worksheet_Calculate()
       Dim c As Range     
    Application.EnableEvents = False
        For Each c In Range("A11:A110") 
                    If c.Value = "" Then
                Rows(c.Row & ":" & c.Row).EntireRow.Hidden = True
            Else
                Range(c.Row & ":" & c.Row).EntireRow.Hidden = False
            End If
        Next

    Application.EnableEvents = True

End Sub
 
Upvote 0
How do I add this speed up code to this one (I'm new to VBA, sorry ?)
You are nearly there already. ScreenUpdating is the one that is likely to have the biggest impact for you.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim c As Range
    
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .EnableEvents = False
    End With
    
    For Each c In Range("A11:A110")
        If c.Value = "" Then
            Rows(c.Row & ":" & c.Row).EntireRow.Hidden = True
        Else
            Range(c.Row & ":" & c.Row).EntireRow.Hidden = False
        End If
    Next
    
    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .EnableEvents = True
    End With

End Sub
 
Upvote 0
I added the below which cut the time down to around 30 seconds. Much better. If anyone else has some tips, please share. Thank you.
I don't know if you got back to this but I don't understand your inner loop "For i = 1 To Range("X" & x)"
eg
1) Columns("Z:Z").NumberFormat = "m/d/yyyy"
This only needst to be done at the end and putting it inside a loop adds no value at all.

2) The next 2 statements rely on x but the output relies on i, so it seems to me that the output cells are only going to contain the value based on the last x.
So why loop through x.
But without sample data I can't be sure.
 
Upvote 0
You are nearly there already. ScreenUpdating is the one that is likely to have the biggest impact for you.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim c As Range
   
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .EnableEvents = False
    End With
   
    For Each c In Range("A11:A110")
        If c.Value = "" Then
            Rows(c.Row & ":" & c.Row).EntireRow.Hidden = True
        Else
            Range(c.Row & ":" & c.Row).EntireRow.Hidden = False
        End If
    Next
   
    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .EnableEvents = True
    End With

End Sub
Thanks. I tried it but it didn't fix the speed for me.
 
Upvote 0
Thanks. I tried it but it didn't fix the speed for me.
You really need to start your own separate thread. It runs fairly quickly as far as I can tell so you will to need to give more of an explanation of what issues you are having.
 
Upvote 0
You really need to start your own separate thread. It runs fairly quickly as far as I can tell so you will to need to give more of an explanation of what issues you are having.
I did, but i didn't get any feedback from anybody.

 
Upvote 0
In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.
Thread closed.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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