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
What am i doing wrong here?
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