ShelleyNeuerburg
asked on
Compare 2 worksheets and delete rows not contained in both.
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 CompareWorksheets()
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("CurrentYear")
Set wsPreviousYear = Worksheets("PreviousYear")
'---- Current Year Worksheet
With wsCurrentYear
wsCurrentYearLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngCurrent = .Range("a2:a" & wsCurrentYearLastRow)
End With
'---- Previous Year Worksheet
With wsPreviousYear
wsPreviousYearLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngPrevious = .Range("a8:a" & wsPreviousYearLastRow)
Set rngDelete = .Range("q8:q" & wsPreviousYearLastRow)
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
wsPreviousYearNextRow = wsPreviousYearLastRow + 1
For Each cell In rngCurrent
res = Application.Match(cell, rngPrevious, 0)
If IsError(res) Then ' Add to end of Previous Year
wsPreviousYear.Cells(wsPre viousYearN extRow, "A") = cell.Value
wsPreviousYear.Cells(wsPre viousYearN extRow, "R") = "Added"
wsPreviousYearNextRow = wsPreviousYearNextRow + 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.Match(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:=xlValues)
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 CompareWorksheets()
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("CurrentYear")
Set wsPreviousYear = Worksheets("PreviousYear")
'---- Current Year Worksheet
With wsCurrentYear
wsCurrentYearLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngCurrent = .Range("a2:a" & wsCurrentYearLastRow)
End With
'---- Previous Year Worksheet
With wsPreviousYear
wsPreviousYearLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngPrevious = .Range("a8:a" & wsPreviousYearLastRow)
Set rngDelete = .Range("q8:q" & wsPreviousYearLastRow)
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
wsPreviousYearNextRow = wsPreviousYearLastRow + 1
For Each cell In rngCurrent
res = Application.Match(cell, rngPrevious, 0)
If IsError(res) Then ' Add to end of Previous Year
wsPreviousYear.Cells(wsPre
wsPreviousYear.Cells(wsPre
wsPreviousYearNextRow = wsPreviousYearNextRow + 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.Match(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.
'---- 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:=xlValues)
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.
ASKER
This partially works. It does not delete the last row that it should. On your example it should add 5, 8 and 9 to the previous year worksheet (which it does) and it should delete 10 & 11 on the previous year worksheet, but it only deletes 10. On my worksheets, it deletes all but the very last one.
ASKER
Upon further testing, it doesn't delete all but the last one, if the rows that need to be deleted are in consecutive rows, it skips every other one.
Example:
If it should delete rows 5, 15, 25, 26, 27, 28, 29, and 35,
it deletes 5, 15, 25, 27, 29 and 35. It skips rows 26 and 28.
Example:
If it should delete rows 5, 15, 25, 26, 27, 28, 29, and 35,
it deletes 5, 15, 25, 27, 29 and 35. It skips rows 26 and 28.
Hello,
I just modified your code
I will put mine when I write it (hopefully soon)
I just modified your code
I will put mine when I write it (hopefully soon)
Hello,
I re-examined the code, it looks OK, if you have a problem in some file PLEASE upload the file to this question
I re-examined the code, it looks OK, if you have a problem in some file PLEASE upload the file to this question
ASKER
How do I upload a file with this question?
it is easy, just goto
http://www.ee-stuff.com/Expert
and login with the same account as this site
then click upload new file
http://www.ee-stuff.com/Expert
and login with the same account as this site
then click upload new file
Just a thought with why it only deletes every other consecutive row...
Once a row gets deleted, the next row down becomes the current row, so when your code deletes a row, the next row becomes the current row, but the code may think it has already deleted that row, so it moves on, thus skipping that row that used to be the next row. In other words... after you delete a row, you need to check the same row again in case the next row down has moved up and has become the current row.
I did not study the code, but I have had this problem before, so it's just a thought in case it helps...
Cheers!
ahammar
Once a row gets deleted, the next row down becomes the current row, so when your code deletes a row, the next row becomes the current row, but the code may think it has already deleted that row, so it moves on, thus skipping that row that used to be the next row. In other words... after you delete a row, you need to check the same row again in case the next row down has moved up and has become the current row.
I did not study the code, but I have had this problem before, so it's just a thought in case it helps...
Cheers!
ahammar
Hello,
Here is the code I used to delete the rows, I used "for each" and then cell.row :
'---- 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.Match(cell, rngCurrent, 0)
If IsError(res) Then
'---- IF IN PREVIOUS YEAR AND NOT IN CURRENT YEAR
'Just Delete the row NOW
wsPreviousYear.Rows(cell.R ow).Delete
End If
Next cell
Here is the code I used to delete the rows, I used "for each" and then cell.row :
'---- 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.Match(cell, rngCurrent, 0)
If IsError(res) Then
'---- IF IN PREVIOUS YEAR AND NOT IN CURRENT YEAR
'Just Delete the row NOW
wsPreviousYear.Rows(cell.R
End If
Next cell
ASKER
I uploaded my file so you could take a look at it.
Thanks.
Thanks.
ASKER
Here is the file. It's doing exactly what ahammar explains, but I don't know how to write the code to take care of this problem. After deleting the row, I tried selecting the previous row and then going forward again, but this did not work. I can run the same code several times to take care of the problem, but I wouldn't know exactly how many times to run it, so I would like to fix it correctly.
Thanks.
View all files for Question ID: 22078645
https://filedb.experts-exchange.com/incoming/ee-stuff/1601-DeleteRows.ZIPhttps://filedb.experts-exchange.com/incoming/ee-stuff/1607-mytestupload_mod.zip
https://filedb.experts-exchange.com/incoming/ee-stuff/1577-compair.zip
Thanks.
View all files for Question ID: 22078645
https://filedb.experts-exchange.com/incoming/ee-stuff/1601-DeleteRows.ZIPhttps://filedb.experts-exchange.com/incoming/ee-stuff/1607-mytestupload_mod.zip
https://filedb.experts-exchange.com/incoming/ee-stuff/1577-compair.zip
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I uploaded a sample file with the code.
To download the file, you must be logged into EE-Stuff. Here are two pages that will display your file, if logged in:
View all files for Question ID: 22078645
https://filedb.experts-exchange.com/incoming/ee-stuff/1601-DeleteRows.ZIPhttps://filedb.experts-exchange.com/incoming/ee-stuff/1607-mytestupload_mod.zip
https://filedb.experts-exchange.com/incoming/ee-stuff/1577-compair.zip
Direct link to your file
https://filedb.experts-exchange.com/incoming/ee-stuff/1577-compair.zip