Delete row if "cell.Value" equals a cell in a range

tonye104292

New Member
Joined
Jul 5, 2013
Messages
4
Hi all, long time reader, first time poster.

I have the code below (not mine but it runs/works fine) that basically deletes a row in a sheet if a cell in that row equals a specific value (in this case, if it equals Total World, Europe, North, or South).

Im wondering if there is a way to modify the code below so if cell.Value equals a cell in a range in a worksheet, then it deletes that row. This would be instead of me specifying in the code "cell.Value = abc OR cell.Value = def" in order to save time and not specify in the code the hundreds of values that I want to add.

Essentially, I want to post the values in the quotes below "Total World, Europe, North, South" in a range in a worksheet, rather than in the code. Highlighted in red is what I want to modify.
Code:
Sub DeleteTotalClusterRows()
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("H:H"), ActiveSheet.UsedRange)
For Each cell In rng
[COLOR=#ff0000]If (cell.Value) = "Total World" Or (cell.Value) = "Europe," Or (cell.Value) = "North" Or (cell.Value) = "South" _[/COLOR]
Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Rich (BB code):
Sub DeleteTotalClusterRows()
Dim rng As Range, cell As Range, del As Range
dim sCriteria as string
Set rng = Intersect(Range("H:H"), ActiveSheet.UsedRange)
scriteria = range("A1")
For Each cell In rng
If (cell.Value) = scriteria Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete

Change range ("A1") to the cell your criteria will be in.
End Sub
 
Upvote 0
Rich (BB code):
Sub DeleteTotalClusterRows()
Dim rng As Range, cell As Range, del As Range
dim sCriteria as string
Set rng = Intersect(Range("H:H"), ActiveSheet.UsedRange)
scriteria = range("A1")
For Each cell In rng
If (cell.Value) = scriteria Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete

Change range ("A1") to the cell your criteria will be in.
End Sub
But I wont just be checking each cell against 1 value, it'll be more like a few thousand values which is why I want it to lookup the values in a table, or range, not just 1 cell.
 
Upvote 0
Rich (BB code):
Sub DeleteTotalClusterRows()
Dim rng As Range, cell As Range, del As Range
dim sCriteria as string
dim i as long
Set rng = Intersect(Range("H:H"), ActiveSheet.UsedRange)
for i = 1 to 10
scriteria = cells(i, 1)
For Each cell In rng
If (cell.Value) = scriteria Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
next i
end sub

This will now loop from A1:A10 and delete from your range if any the column H value = anything in A1:A10

Adjust as needed.
 
Upvote 0
hi try below code Put your criteria in Sheet2 Column A or you can change the range ( Highlighted) to where your criteria listed
Rich (BB code):
Option Explicit
Sub DeleteTotalClusterRows()
Dim rng As Range, cell As Range, del As Range, lkrng As Range
Set rng = Intersect(Range("H:H"), ActiveSheet.UsedRange)
Set lkrng = Sheets("Sheet2").Range("A:A")
For Each cell In rng
   On Error Resume Next
   If WorksheetFunction.Match(cell.Value, lkrng, False) > 0 Then
      If Err.Number = 1004 Then
        GoTo Nextt
      Else
        If del Is Nothing Then
          Set del = cell
          Else: Set del = Union(del, cell)
        End If
      End If
   End If
Nextt:
Next cell
On Error Resume Next
del.EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,415
Messages
6,124,768
Members
449,187
Latest member
hermansoa

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