Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year Worksheet
Compare Previous Year Worksheet with Current Year Worksheet and if SSN exists in Previous Year Worksheet & Not in Current Year Worksheet - Delete this Row out of Previous Year Worksheet - THIS IS WHERE I'M HAVING TROUBLE.
This is the code I have so far. I just don't know how to delete the row.
Sub CompareWorkshee ts()
Dim wsCurrentYear As Worksheet, wsPreviousYear As Worksheet
Dim rngCurrent As Range, rngPrevious As Range
Dim LastRow As Long
Dim res As Variant
Set wsCurrentYear = Worksheets("Cur rentYear")
Set wsPreviousYear = Worksheets("Pre viousYear")
'---- Current Year Worksheet
With wsCurrentYear
wsCurrentYearLa stRow = .Cells(Rows.Cou nt, 1).End(xlUp).Ro w
Set rngCurrent = .Range("a2:a" & wsCurrentYearLa stRow)
End With
'---- Previous Year Worksheet
With wsPreviousYear
wsPreviousYearL astRow = .Cells(Rows.Cou nt, 1).End(xlUp).Ro w
Set rngPrevious = .Range("a8:a" & wsPreviousYearL astRow)
Set rngDelete = .Range("q8:q" & wsPreviousYearL astRow)
End With
'---- Compare Current Year Worksheet with Previous Year Worksheet
'---- If SSN exists in Current Year Worksheet & Not in Previous Year -
'---- Copy this Row from Current Year Worksheet & Paste into Previous Year Worksheet
wsPreviousYearN extRow = wsPreviousYearL astRow + 1
For Each cell In rngCurrent
res = Application.Mat ch(cell, rngPrevious, 0)
If IsError(res) Then ' Add to end of Previous Year
wsPreviousYear. Cells(wsPreviou sYearNextRow, "A") = cell.Value
wsPreviousYear. Cells(wsPreviou sYearNextRow, "R") = "Added"
wsPreviousYearN extRow = wsPreviousYearN extRow + 1
End If
Next cell
'---- Compare Previous Year Worksheet with Current Year Worksheet
'---- If SSN exists in Previous Year Worksheet & Not in Current Year Worksheet -
'---- Delete this Row out of Previous Year Worksheet
For Each cell In rngPrevious
res = Application.Mat ch(cell, rngCurrent, 0)
If IsError(res) Then
'---- IF IN PREVIOUS YEAR AND NOT IN CURRENT YEAR, INSERT THE WORD
'---- "DELETE" IN COLUMN Q SO THAT ROW CAN BE DELETED
wsPreviousYear. Cells(cell.Row, "Q") = "Delete"
'---- I WOULD REALLY RATHER DELETE THE ROW HERE IN LIEU OF ADDING THE
'---- WORD "DELETE" TO THE ROW & COLUMN Q
End If
Next cell
'---- SINCE I DIDN'T KNOW HOW TO GET IT TO DELETE THE ROW ABOVE, I TRIED
'---- DELETING THE ROWS THAT HAVE THE WORD DELETE ADDED TO COLUMN Q
With rngDelete
Set c = .Find("Delete", LookIn:=xlValue s)
If Not c Is Nothing Then
firstAddress = c.Address
Do
'---- Change the word "Delete" to "Delete Row" just to see if this is working properly!
'---- IF I TRY TO DELETE THE ROWS (IN LIEU OF CHANGE THE VALUE TO "DELETE ROW"
'---- THEN IT BOMBS ON - Set c = FindNext(c)
c.Value = "Delete Row"
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
I REALLY NEED YOUR HELP ASAP.
THANKS.
Compare Previous Year Worksheet with Current Year Worksheet and if SSN exists in Previous Year Worksheet & Not in Current Year Worksheet - Delete this Row out of Previous Year Worksheet - THIS IS WHERE I'M HAVING TROUBLE.
This is the code I have so far. I just don't know how to delete the row.
Sub CompareWorkshee ts()
Dim wsCurrentYear As Worksheet, wsPreviousYear As Worksheet
Dim rngCurrent As Range, rngPrevious As Range
Dim LastRow As Long
Dim res As Variant
Set wsCurrentYear = Worksheets("Cur rentYear")
Set wsPreviousYear = Worksheets("Pre viousYear")
'---- Current Year Worksheet
With wsCurrentYear
wsCurrentYearLa stRow = .Cells(Rows.Cou nt, 1).End(xlUp).Ro w
Set rngCurrent = .Range("a2:a" & wsCurrentYearLa stRow)
End With
'---- Previous Year Worksheet
With wsPreviousYear
wsPreviousYearL astRow = .Cells(Rows.Cou nt, 1).End(xlUp).Ro w
Set rngPrevious = .Range("a8:a" & wsPreviousYearL astRow)
Set rngDelete = .Range("q8:q" & wsPreviousYearL astRow)
End With
'---- Compare Current Year Worksheet with Previous Year Worksheet
'---- If SSN exists in Current Year Worksheet & Not in Previous Year -
'---- Copy this Row from Current Year Worksheet & Paste into Previous Year Worksheet
wsPreviousYearN extRow = wsPreviousYearL astRow + 1
For Each cell In rngCurrent
res = Application.Mat ch(cell, rngPrevious, 0)
If IsError(res) Then ' Add to end of Previous Year
wsPreviousYear. Cells(wsPreviou sYearNextRow, "A") = cell.Value
wsPreviousYear. Cells(wsPreviou sYearNextRow, "R") = "Added"
wsPreviousYearN extRow = wsPreviousYearN extRow + 1
End If
Next cell
'---- Compare Previous Year Worksheet with Current Year Worksheet
'---- If SSN exists in Previous Year Worksheet & Not in Current Year Worksheet -
'---- Delete this Row out of Previous Year Worksheet
For Each cell In rngPrevious
res = Application.Mat ch(cell, rngCurrent, 0)
If IsError(res) Then
'---- IF IN PREVIOUS YEAR AND NOT IN CURRENT YEAR, INSERT THE WORD
'---- "DELETE" IN COLUMN Q SO THAT ROW CAN BE DELETED
wsPreviousYear. Cells(cell.Row, "Q") = "Delete"
'---- I WOULD REALLY RATHER DELETE THE ROW HERE IN LIEU OF ADDING THE
'---- WORD "DELETE" TO THE ROW & COLUMN Q
End If
Next cell
'---- SINCE I DIDN'T KNOW HOW TO GET IT TO DELETE THE ROW ABOVE, I TRIED
'---- DELETING THE ROWS THAT HAVE THE WORD DELETE ADDED TO COLUMN Q
With rngDelete
Set c = .Find("Delete", LookIn:=xlValue s)
If Not c Is Nothing Then
firstAddress = c.Address
Do
'---- Change the word "Delete" to "Delete Row" just to see if this is working properly!
'---- IF I TRY TO DELETE THE ROWS (IN LIEU OF CHANGE THE VALUE TO "DELETE ROW"
'---- THEN IT BOMBS ON - Set c = FindNext(c)
c.Value = "Delete Row"
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
I REALLY NEED YOUR HELP ASAP.
THANKS.
Comment