Macro running so slow

Hiport

Active Member
Joined
May 9, 2008
Messages
455
Hi, the below code is taking forever to run, the worksheet has no formulas, so dont know why its running so slow, i do have 3 other worksheets in the workbook, in which one of those worksheets which does have formulas, should this be the reason?

Is there anything i can do to make it run faster?


Code:
Sub Delete_Dupes()
Dim rw1 As Long: rw1 = 1
Dim rwx As Long: rwx = rw1
Dim stepx As Integer
Dim co1 As Integer: co1 = 3
Dim bool As Boolean

Application.ScreenUpdating = False
Do Until Cells(rwx, co1) = ""
    stepx = 1
    If rwx > rw1 Then
        On Error GoTo NewCrit
        bool = IsError(Application.WorksheetFunction.Match(Cells(rwx, co1), Range(Cells(rw1, co1), Cells(rwx - 1, co1)), 0))
        Select Case bool
            Case True
                'no match found thus row = first instance (no delete)
            Case False
                'match found in prior rows therefore duplicate so delete
                Rows(rwx).Delete
                stepx = 0
        End Select
    End If
rwx = rwx + stepx
Loop
Exit Sub
NewCrit:
bool = True
Resume Next
 
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Rich (BB code):
Sub Delete_Dupes()
Dim rw1 As Long: rw1 = 1
Dim rwx As Long: rwx = rw1
Dim stepx As Integer
Dim co1 As Integer: co1 = 3
Dim bool As Boolean
 
Application.ScreenUpdating = False
Do Until Cells(rwx, co1) = ""
    stepx = 1
    If rwx > rw1 Then
        On Error GoTo NewCrit
     bool = IsError(Application.WorksheetFunction.Match(Cells(rwx, co1), Range(Cells(rw1, co1), Cells(rwx - 1, co1)), 0))
     Select Case bool
         Case True
             'no match found thus row = first instance (no delete)
         Case False
             'match found in prior rows therefore duplicate so delete
             Rows(rwx).Delete
             stepx = 0
     End Select
    End If
rwx = rwx + stepx
Loop
Exit Sub
NewCrit:
bool = True
Resume Next
 
Application.ScreenUpdating = True
End Sub
The read part doesn't make sense at all.
You can not evaluate the Error value by IsError function resulting from Application.WorksheetFunction.Match, because it raises a RunTime Error when it doesn't find matched value.
If you want to return the error value, you need to use Application.Match instead.
But...
Why you need to use the function ?
try
Rich (BB code):
Sub deldup()
Dim i As Long
Application.ScreenUpdating = False
For i = Cells(Rows.Count).End(xlUp).Row To 2 Step - 1
    If Cells(i - 1, 1).Value = Cells(1, 1).Value Then Rows(i).Delete
Next
End Sub
or
Rich (BB code):
Sub test()
Columns(1).Insert
With Range("b1", Range("b" & Rows.Count).End(xlUp)).Offset(,-1)
    .Formula = "=if(countif(b$1:b1,b1)=1,1,"""")"
    .SpecialCells(-4123,1).EntireRow.Delete
End With
Columns(1).Delete
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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