Speed Up Macro Segment

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a very long macro that's taking a REALLY long time to run (so long that my computer goes into standby mode while it runs). I narrowed the problem area of the code down to the segment below, and am wondering if anyone has any tips on how to make this more efficient. It does what it's supposed to do, it's just too slow to even use. The "Overall User Data" sheet has just over 10k lines and about 800 lines will be deleted by this code.

VBA Code:
yr = ThisWorkbook.Sheets("New Data Add").Range("A2").Value
mth = ThisWorkbook.Sheets("New Data Add").Range("B2").Value

'Delete rows from the previous year of the month of data that was just added.

For r = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count To 1 Step -1
    If ThisWorkbook.Sheets("Overall User Data").Cells(r, "A") = yr - 1 Then
        If ThisWorkbook.Sheets("Overall User Data").Cells(r, "B") = mth Then
            ThisWorkbook.Sheets("Overall User Data").Rows(r).EntireRow.Delete
        End If
    End If
Next

I think I read somewhere that you can select all the lines you want to delete, then delete the selection at the end and that the act of selecting rather than deleting would help macros run faster. I'm not sure if this would work for entire rows, or if it's even a valid idea.

Any advice anyone can give would be much appreciated!

Kate
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi

Use

VBA Code:
yr = ThisWorkbook.Sheets("New Data Add").Range("A2").Value
mth = ThisWorkbook.Sheets("New Data Add").Range("B2").Value

    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .EnableCancelKey = xlErrorHandler
    End With
    ActiveSheet.DisplayPageBreaks = False

'Delete rows from the previous year of the month of data that was just added.

For r = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count To 1 Step -1
    If ThisWorkbook.Sheets("Overall User Data").Cells(r, "A") = yr - 1 Then
        If ThisWorkbook.Sheets("Overall User Data").Cells(r, "B") = mth Then
            ThisWorkbook.Sheets("Overall User Data").Rows(r).EntireRow.Delete
        End If
    End If
Next
    
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .StatusBar = False
        .EnableCancelKey = xlInterrupt
    End With
Next
 
Upvote 0
Much better! It still takes about 5 mins to run, but that's more manageable. I did have my screen updating turned off, but adding those other settings seems to have helped.

I'm wondering, should I add those settings to the very beginning and end of the entire macro rather than just the segment? Are there any negative consequences that can occur with any of these settings that I should watch out for if I do choose to add them?
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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