Code Crashing Sometimes

termeric

Active Member
Joined
Jun 21, 2005
Messages
280
Hello, i'm trying to figure out why my code crashes sometimes, but works othertimes, seemingly at random.

I am comparing the current roster to the last roster so i can build a list of people who has departed. i add people to the end of the departures list, adn then remove any duplicates, so someone can only be on there once. I'm removing duplicates based on column 1, which is employee ID.

My code crashes sometimes when the Departures sheet is blank, but also sometimes when it is not blank. sometimes after it crashes i click the play button to resume and it just works, and other times it just continues to crash. The error message i get is Application-defined or object-defined error.

this is the line that it debugs to
Code:
Sheets("Departures").Range(Cells(1, 1), Cells(r, c)).RemoveDuplicates Columns:=1, Header:=xlYes

What am i doing wrong here?

Code:
 Sub FindDifferences()
 'compares Roster to Last Roster to generate the Departures
 'this tab does not clear, it just builds on what was there before
 
    Dim i As Long, ii As Long, sq1 As Variant, sq2 As Variant, m As Long, r As Long, c As Long, v As Long
    

    r = Sheets("Departures").UsedRange.Rows.Count
    
    sq1 = Sheets("Roster").Cells(1).CurrentRegion.Columns(1)
    sq2 = Sheets("LastRoster").Cells(1).CurrentRegion.Columns(1)

    Sheets("LastRoster").Rows(1).Copy Sheets("Departures").Range("A1")


    For i = 1 To UBound(sq2)
        ii = 0
        On Error Resume Next
        ii = Application.Match(sq2(i, 1), sq1, 0)
        On Error GoTo 0
        If ii = 0 Then
            m = m + 1
             Sheets("LastRoster").Rows(i).Copy Sheets("Departures").Range("A" & m + r)
        End If
    Next
    
    'get the new row and col values
    r = Sheets("Departures").UsedRange.Rows.Count
    c = Sheets("Departures").UsedRange.Columns.Count
    

    Sheets("Departures").Range(Cells(1, 1), Cells(r, c)).RemoveDuplicates Columns:=1, Header:=xlYes

'Delete blank rows
    For v = 1 To r
        If Sheets("Departures").Range("A" & v).Value = "" Then Sheets("departures").Range("A" & v).EntireRow.Delete
    Next v

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I could see you getting that message if r=0, r=1, or c=0 as you are saying that the data has a header row.
So that would mean that there is no data.

Confirm what r and c are when you get the error (hover over the "r" and "c" variables and it should tell you what their value is at that point in time).
 
Upvote 0
My headings row is never deleted, so sometimes r = 1. I just cleared the actual data and its currently running fine with r =1 and c = 43. however, when i update my data first, which runs this subroutine after i import new data, its crashing again with r = 1 and c = 43. And then after i click debug, if i hit play, it finishes with out erroring again.
 
Upvote 0
My headings row is never deleted, so sometimes r = 1. I just cleared the actual data and its currently running fine with r =1 and c = 43. however, when i update my data first, which runs this subroutine after i import new data, its crashing again with r = 1 and c = 43. And then after i click debug, if i hit play, it finishes with out erroring again.
If r=1, then I WOULD expect an error.
That means that all you have is the header, and no rows of data. It cannot remove duplicates from a range with no rows of data!
If r = 1, then it means you only have a header and no rows of data!
So either your calculation of r is wrong, or you have no data.

If you sometimes have no data, then you can skip over the remove duplicates line of code by wrapping that line of code inside and IF/THEN statement this:
VBA Code:
If r > 1 Then
    Sheets("Departures").Range(Cells(1, 1), Cells(r, c)).RemoveDuplicates Columns:=1, Header:=xlYes
End If
 
Upvote 0
If r=1, then I WOULD expect an error.
That means that all you have is the header, and no rows of data. It cannot remove duplicates from a range with no rows of data!
If r = 1, then it means you only have a header and no rows of data!
So either your calculation of r is wrong, or you have no data.

If you sometimes have no data, then you can skip over the remove duplicates line of code by wrapping that line of code inside and IF/THEN statement this:
VBA Code:
If r > 1 Then
    Sheets("Departures").Range(Cells(1, 1), Cells(r, c)).RemoveDuplicates Columns:=1, Header:=xlYes
End If
So i had actually wrapped it in an if statement earlier today when i was trying to figure out the reason for the crash, but it didnt seem to fix the issue so i removed it.
 
Upvote 0
Try specifying the sheet on Cells, as you would have got an error if Sheets("Departures") wasn't the activesheet.
Rich (BB code):
Range(Sheets("Departures").Cells(1, 1), Sheets("Departures").Cells(r, c)).RemoveDuplicates Columns:=1, Header:=xlYes
 
Upvote 0
Solution

Forum statistics

Threads
1,223,227
Messages
6,170,847
Members
452,361
Latest member
d3ad3y3

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