VBA code to compare 2 sheets and delete rows based on 2 criteria

tervozina

New Member
Joined
Feb 18, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have 2 sheets: 1 sheet has 4 columns, 2 sheet contains 3 columns. Column B on both sheets has document numbers. Column D on Sheet1 has document status.
So, I want to compare 2 sheets and delete rows from Sheet1 which have status (column D) "Settled" in Sheet1 plus do not match document numbers in column B from Sheet2.

I have the code to compare 2 sheets to delete rows from the Sheet1 if document number is not on Sheet2, but I cannot figure out how to incorporate second condition with "Settled" status in column D in Sheet1 to be deleted.
The point is, I want the rows in Sheet1 to remain only if they are a match to document number in Sheet2 and have any other status than "Settled", so even if they are not in Sheet2 but have a status anything else than "Settled", they should not be deleted.

Hope that makes sense?

Any help would be appreciated.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
When you're looking at the VBA code, if you click on View in the menu bar and select Locals Window a window that shows all of the variable values will pop up. Put your cursor in the code somewhere click and then hit F8. Just before the code executes that line that it errors on, check what the value of X is in the locals window.
 
Upvote 0
When you're looking at the VBA code, if you click on View in the menu bar and select Locals Window a window that shows all of the variable values will pop up. Put your cursor in the code somewhere click and then hit F8. Just before the code executes that line that it errors on, check what the value of X is in the locals window.
Value is 0 for i, x and y
 
Upvote 0
Try changing the line
set wkbk = ThisWorkbook to set wkbk = Workbooks("YourWorkbookName")
 
Upvote 0
Try changing the line
set wkbk = ThisWorkbook to set wkbk = Workbooks("YourWorkbookName")
It needs to be the full name. i.e.- WorkbookName.xlsm (or whatever your file extension is)
 
Upvote 0
It needs to be the full name. i.e.- WorkbookName.xlsm (or whatever your file extension is)
And always remember to save an unmolested version of your Workbook in case the code gives unwanted results. ;)
 
Upvote 0
Maybe it has something to do with the formatting on this site. I have included the code again with a VBA format for copy and paste. Maybe??

VBA Code:
Sub compareData()
Dim sht1 As Worksheet, sht2 As Worksheet
Dim wkbk As Workbook
Dim checkRng As Range, searchRng As Range
Dim i As Integer, x As Integer, y As Integer

Set wkbk = Workbooks("WorkbookName")
Set sht1 = wkbk.Worksheets("Sheet1Name")
Set sht2 = wkbk.Worksheets("Sheet2Name")

x = sht1.UsedRange.Rows.Count + sht1.UsedRange.Rows(1).Row - 1
y = sht2.UsedRange.Rows.Count + sht2.UsedRange.Rows(1).Row - 1

Set checkRng = Range(sht1.Cells(2, 2), sht1.Cells(x, 2))
Set searchRng = Range(sht2.Cells(2, 2), sht2.Cells(y, 2))

i = x - 1

Do Until i = 0
   If searchRng.Find(checkRng.Cells(i, 1).Value) Is Nothing Or sht1.Cells(i + 1, 4).Value = "Settled" Then
        sht1.Rows(i + 1).Delete
    End If
    i = i - 1
Loop

End Sub
 
Upvote 0
Solution
Maybe it has something to do with the formatting on this site. I have included the code again with a VBA format for copy and paste. Maybe??

VBA Code:
Sub compareData()
Dim sht1 As Worksheet, sht2 As Worksheet
Dim wkbk As Workbook
Dim checkRng As Range, searchRng As Range
Dim i As Integer, x As Integer, y As Integer

Set wkbk = Workbooks("WorkbookName")
Set sht1 = wkbk.Worksheets("Sheet1Name")
Set sht2 = wkbk.Worksheets("Sheet2Name")

x = sht1.UsedRange.Rows.Count + sht1.UsedRange.Rows(1).Row - 1
y = sht2.UsedRange.Rows.Count + sht2.UsedRange.Rows(1).Row - 1

Set checkRng = Range(sht1.Cells(2, 2), sht1.Cells(x, 2))
Set searchRng = Range(sht2.Cells(2, 2), sht2.Cells(y, 2))

i = x - 1

Do Until i = 0
   If searchRng.Find(checkRng.Cells(i, 1).Value) Is Nothing Or sht1.Cells(i + 1, 4).Value = "Settled" Then
        sht1.Rows(i + 1).Delete
    End If
    i = i - 1
Loop

End Sub
Unfortunately, it does not want to work :(
 

Attachments

  • Capture.JPG
    Capture.JPG
    50.4 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,215,841
Messages
6,127,221
Members
449,371
Latest member
strawberrish

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