VBA to delete row on all worksheets meeting multiple criteria

Deirdre

Board Regular
Joined
Feb 28, 2002
Messages
137
I have a workbook which contains 7 worksheets of various row length. I have a macro that already performs various tasks which works fine but now I would like to add to it by searching every worksheet and deleting any row that contains a 0 or the text NULL in column A.
After a search, I found the following but it only works on the active worksheet.

VBA Code:
Sub test()

   Dim i As Long
    Dim sht As Worksheet
    
    For Each sht In ActiveWorkbook.Sheets
        For i = sht.UsedRange.Rows.Count To 2 Step -1
            If Cells(i, 1).Value = "NULL" OR Cells(i, 1).Value = 0 Then Cells(i, 1).EntireRow.Delete
        Next i
    Next sht
End Sub



1677962063001.png
 

Attachments

  • 1677962230338.png
    1677962230338.png
    15.3 KB · Views: 0
Jeez. I'm sorry. Made the change to one line but not the other. Try replacing the highlighted line with the following.
VBA Code:
Set sht = Workbooks("YourWorkbookName").Worksheets(x)
I'm sorry - I believe this workbook is possessed. With the changes, it does run but still only on the active sheet. It does not remove "NULL" or 0 rows from any other worksheet that has that value in Column A.
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
We're gonna get it. Try this.
VBA Code:
Sub test()

Dim i As Long, x As Integer
Dim sht As Worksheet, wb As Workbook
Set wb = Workbooks("YourWorkbookName")
    
For x = 1 To wb.Worksheets.Count
Set sht = wb.Worksheets(x)
    For i = sht.UsedRange.Rows.Count To 2 Step -1
        If sht.Cells(i, 1).Value = "NULL" Or sht.Cells(i, 1).Value = 0 Then sht.Cells(i, 1).EntireRow.Delete
    Next i
Next x
End Sub
 
Upvote 0
Solution
We're gonna get it. Try this.
VBA Code:
Sub test()

Dim i As Long, x As Integer
Dim sht As Worksheet, wb As Workbook
Set wb = Workbooks("YourWorkbookName")
   
For x = 1 To wb.Worksheets.Count
Set sht = wb.Worksheets(x)
    For i = sht.UsedRange.Rows.Count To 2 Step -1
        If sht.Cells(i, 1).Value = "NULL" Or sht.Cells(i, 1).Value = 0 Then sht.Cells(i, 1).EntireRow.Delete
    Next i
Next x
End Sub
That worked! Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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