Extend VBA code to multiple sheets

Drewsy

New Member
Joined
Feb 6, 2014
Messages
1
Morning all,

I have created a code that allows me to remove rows based on a certain criteria.

My limited VBA skills are now evident, as i need to extend this code across the entire workbook but have come a cropper!

The code is as follows:

Sub Delete_Rows()
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("A16:W40000"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "Not current" _
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

How do i loop the code to cover multiple worksheets?

Any help would be greatly appreciated!

Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Something like this

Code:
Option Explicit


Sub Delete_Rows()
    Dim ws As Worksheet
    Dim rng As Range, cell As Range, del As Range


    For Each ws In ThisWorkbook.Worksheets
        Set rng = Intersect(ws.Range("A16:W40000"), ws.UsedRange)
        For Each cell In rng
            If (cell.Value) = "Not current" _
               Then
                If del Is Nothing Then
                    Set del = cell
                Else: Set del = Union(del, cell)
                End If
            End If
        Next cell
    Next ws
    On Error Resume Next
    del.EntireRow.Delete
End Sub

Depending on the data layout you would probably find automating AutoFilter much faster
 
Upvote 0
Perhaps set your sheets to an array.

This is untested but gives you an idea how to get started. I show three sheets in the array, you can add more. You will need to add the sheets by their actual name and in the format as you see it. "My Sheet Name", (Note the comma delimiter between sheet names only.)


Code:
Option Explicit

Sub Delete_Rows()
 Dim rng As Range, cell As Range, del As Range
  Dim MyArr As Variant
 
 MyArr = Array("Sheet2", "Sheet3", "Sheet4")

  For i = LBound(MyArr) To UBound(MyArr)
     With Sheets(MyArr(i))
         Set rng = Intersect(Range("A16:W40000"), ActiveSheet.UsedRange)
       For Each cell In rng
          If (cell.Value) = "Not current" 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

Regards,
Howard
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,450
Members
449,453
Latest member
jayeshw

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