Repeating Process in New Sheet

JordanW

New Member
Joined
Apr 30, 2011
Messages
3
Hi, I just started working with variables and not sure I understand them. I have the following code that basically seaches a range and if the specified value doesn't appear, the row will be deleted. This works for the first sheet that is referenced but not for the second. Originally, I had defined the variables in both pieces and got an error about "duplicate declaration in current scope". I thought the correct thing to do was to move the dim statements to the top of the code and they only appear once but my code still does not work for the second sheet (although I don't get the error any longer). I'd appreciate some help. Thanks.

Code:
Dim rng As Range, cell As Range, del As Range
  Dim strCellValue As String
  
Sheets("ABC").Select
Set rng = Intersect(Range("D7:D10006"), ActiveSheet.UsedRange)
  For Each cell In rng
    strCellValue = (cell.Value)
    If InStr(strCellValue, "Dog") = 0 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
 
Sheets("XYZ").Select
Set rng = Intersect(Range("D7:D10006"), ActiveSheet.UsedRange)
  For Each cell In rng
    strCellValue = (cell.Value)
    If InStr(strCellValue, "Cat") = 0 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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Your On Error Resume Next may be masking errors. Try like this

Rich (BB code):
Dim rng As Range, cell As Range, del As Range
  Dim strCellValue As String
  
Sheets("ABC").Select
Set rng = Intersect(Range("D7:D10006"), ActiveSheet.UsedRange)
  For Each cell In rng
    strCellValue = (cell.Value)
    If InStr(strCellValue, "Dog") = 0 Then
        If del Is Nothing Then
           Set del = cell
        Else: Set del = Union(del, cell)
        End If
     End If
  Next cell
  If del Is Nothing Then
    MsgBox "Nothing to delete on sheet " & ActiveSheet.Name
   Else
    del.EntireRow.Delete
   End If
 
Sheets("XYZ").Select
Set rng = Intersect(Range("D7:D10006"), ActiveSheet.UsedRange)
  For Each cell In rng
    strCellValue = (cell.Value)
    If InStr(strCellValue, "Cat") = 0 Then
        If del Is Nothing Then
           Set del = cell
        Else: Set del = Union(del, cell)
        End If
     End If
  Next cell
  If del Is Nothing Then
    MsgBox "Nothing to delete on sheet " & ActiveSheet.Name
   Else
    del.EntireRow.Delete
   End If
 
Upvote 0
Try this.
Code:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim rng As Range, cell As Range, del As Range
Dim strCellValue As String
 
    For Each ws In ThisWorkbook.Worksheets
 
        Select Case ws.Name
 
            Case "ABC", "XYZ"
 
                Set rng = Intersect(ws.Range("D7:D10006"), ws.UsedRange)
 
                For Each cell In rng
 
                    strCellValue = (cell.Value)
 
                    If InStr(strCellValue, "Dog") = 0 Then
 
                        If del Is Nothing Then
                            Set del = cell
                        Else
                            Set del = Union(del, cell)
                        End If
 
                    End If
 
                Next cell
 
               If Not del Is Nothing then del.EntireRow.Delete
 
            Case Else
                ' do nothing
        End Select
 
    Next ws
 
End Sub
It'll loop through all the worksheets but will only run the code on the 2 worksheets called ABC and XYZ.

You can easily alter the Select Case part to include/exclude worksheets, and there are probably other approachs.

The most important thing I suppose is the use of ws to refer to the worksheet(s) in the loop and the code.

That makes sure the code is applied to that right worksheet.
 
Upvote 0
Here's the reason for the error. You need to add the code in red after processing the first sheet.

Rich (BB code):
If del Is Nothing Then
    MsgBox "Nothing to delete on sheet " & ActiveSheet.Name
   Else
    del.EntireRow.Delete
   End If
Set del = Nothing
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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