is there a way to improve my current macro to run faster?

jo206

New Member
Joined
Jul 10, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
hi all

I have this macro but it's taking too long, I usually have about 7000 rows. this macro deletes rows in sheet1 based on values in sheet2.

is there a way to improve my current macro to run faster?

thank you!

VBA Code:
Sub XYZ()
Dim LR As Long, i As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = LR To 1 Step -1
        If IsNumeric(Application.Match(.Range("A" & i).Value, Sheets("Sheet2").Columns("A"), 0)) Then .Rows(i).Delete
    Next i
End With
End Sub
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The below should be faster:
VBA Code:
Sub XYZ()
    Dim LR As Long, i As Long, uRng As Range
    With Sheets("Sheet1")
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        For i = LR To 1 Step -1
            If IsNumeric(Application.Match(.Range("A" & i).Value, Sheets("Sheet2").Columns("A"), 0)) Then
                If uRng Is Nothing Then
                    Set uRng = .Range("A" & i)
                Else
                    Set uRng = Union(uRng, .Range("A" & i))
                End If
            End If
        Next i
        uRng.EntireRow.Delete
    End With
End Sub
 
Upvote 0
The below should be faster:
VBA Code:
Sub XYZ()
    Dim LR As Long, i As Long, uRng As Range
    With Sheets("Sheet1")
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        For i = LR To 1 Step -1
            If IsNumeric(Application.Match(.Range("A" & i).Value, Sheets("Sheet2").Columns("A"), 0)) Then
                If uRng Is Nothing Then
                    Set uRng = .Range("A" & i)
                Else
                    Set uRng = Union(uRng, .Range("A" & i))
                End If
            End If
        Next i
        uRng.EntireRow.Delete
    End With
End Sub
And turn your screen updating off as well..
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

I think that you would find this much faster again. (Don't be put off by the length of the code ;))
Test with a copy of your workbook.

VBA Code:
Sub XYZ_v2()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet2")
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
  End With
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  With Sheets("Sheet1")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If d.exists(a(i, 1)) Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A1").Resize(UBound(a), nc)
        .Columns(nc).Value = b
        .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.ScreenUpdating = True
    End If
  End With
  MsgBox "Done" 'You could remove this line
End Sub

Edit: Just did a timing test with 7,000 rows where about half (scattered throughout) get deleted.
Post #2: 19.9 seconds
This code: 0.04 seconds
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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