Excel VBA Delete the Entire Row if Value Matches the Cell in Another Sheet

Chang123

New Member
Joined
Jul 7, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Web
Hey there, I have a workbook that contains two works sheets, let's say Sheet1 and Sheet2.

What I want is that for the cells in Sheet2 Column A, if the cell value equals to the Sheet1 Cell D5, then delete the entire row.

Can you help to let me know how to make it with VBA? Thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about:

VBA Code:
Sub DeleteRowIfValue()
'
    Dim LastRowInRange  As Long, RowCounter As Long
'
    LastRowInRange = Sheets("Sheet2").Range("A:A").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row            ' Returns a Row Number
'
    For RowCounter = LastRowInRange To 1 Step -1                                            ' Count Backwards
        If Sheets("Sheet2").Range("A" & RowCounter) = Sheets("Sheet1").Range("D5") Then     '   If Cell matches our 'Delete if' value then
            Sheets("Sheet2").Rows(RowCounter).EntireRow.Delete                              '       Delete the row
        End If
    Next
End Sub
 
Upvote 0
How about:

VBA Code:
Sub DeleteRowIfValue()
'
    Dim LastRowInRange  As Long, RowCounter As Long
'
    LastRowInRange = Sheets("Sheet2").Range("A:A").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row            ' Returns a Row Number
'
    For RowCounter = LastRowInRange To 1 Step -1                                            ' Count Backwards
        If Sheets("Sheet2").Range("A" & RowCounter) = Sheets("Sheet1").Range("D5") Then     '   If Cell matches our 'Delete if' value then
            Sheets("Sheet2").Rows(RowCounter).EntireRow.Delete                              '       Delete the row
        End If
    Next
End Sub
Thank you! I tried this code. While the issue is that when I run the code, the Excel just had no reaction (the row didn't delete, and there's also no error warning). I have never met this kind of situation before, so I don't know where's the issue in this case.
 
Upvote 0
I verified the code works. Did you change the sheet names in the code to reflect your sheet names being used?

Sheet1 that has the value to search for in D5
Sheet2 The values in column A that are searched through
 
Upvote 0
I verified the code works. Did you change the sheet names in the code to reflect your sheet names being used?

Sheet1 that has the value to search for in D5
Sheet2 The values in column A that are searched through
Yes, I did, and that's weird.
 
Upvote 0
I verified the code works. Did you change the sheet names in the code to reflect your sheet names being used?

Sheet1 that has the value to search for in D5
Sheet2 The values in column A that are searched through
I found the reason. It was because the format of Sheet1 D5 was Text. After I changed it to General, the issue was fixed. Thanks!
 
Upvote 0
Hi, I need to find D:D and delete in excel sheet2. not only D5 value. please help me. thank you
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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