VBA: Remove rows based on date in cell in another worksheet

Declamatory

Active Member
Joined
Nov 6, 2014
Messages
319
Good afternoon,

I have a workbook with to worksheets. Sheet 1 has a list of items. In column P is the date of payment.

In Sheet 2 I have payment date in cell C25.

Can anybody help me with vba code that would remove the rows in sheet 1 that don't have the date in cell C25 in Sheet 2 in column P?

Thanks in advance for any help you can give me.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This is code that I use to delete rows in another macro I run that deletes the rows that have "Buy" in column B. I just need to somehow do the same but rather than "Buy" looking at the contents of cell C25 in Sheet 2.
Code:
  Last = Cells(Rows.Count, "M").End(xlUp).Row
       
    For i = Last To 1 Step -1
        If (Cells(i, "B").Value) = "Buy" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
 
Upvote 0
Try this:

Code:
  Last = Cells(Rows.Count, "M").End(xlUp).Row
       
    For i = Last To 1 Step -1
        If worksheets("Sheet2".range("C25").Value <> (Cells(i, "B").Value Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
 
Upvote 0
Thanks for replying. I got a compile error:

Expected: list separator or )

The error was in the line

If worksheets("Sheet2".range("C25").Value <> (Cells(i, "B").Value Then
 
Upvote 0
sorry, that should be If worksheets("Sheet2").range("C25").Value <> (Cells(i, "B").Value Then
 
Upvote 0
Thanks for your help. I think there should have been another ) after .Value

I really appreciate it, thanks.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

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