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

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.
Try this. Small change to your code.
VBA Code:
Sub test()

   Dim i As Long, x As Integer
    Dim sht As Worksheet
    
    For x = 1 To ThisWorkbook.Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(x)
        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 x
End Sub
 
Upvote 0
Try this. Small change to your code.
VBA Code:
Sub test()

   Dim i As Long, x As Integer
    Dim sht As Worksheet
   
    For x = 1 To ThisWorkbook.Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(x)
        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 x
End Sub
Thank you for the reply. It did not work - not even on the active sheet.
 
Upvote 0
I replaced the code I have above in the same module. I did not embed it in the greater macro as I wanted to see it run on its own first with my workbook.
1677964933706.png
 
Upvote 0
I replaced the code I have above in the same module. I did not embed it in the greater macro as I wanted to see it run on its own first with my workbook.
View attachment 86761
Ah. I wrote it to work in the Workbook with the data in it. This small change should work. Replace "YourWorkbook" with Workbook Name.
VBA Code:
Sub test()

   Dim i As Long, x As Integer
    Dim sht As Worksheet
    
    For x = 1 To Workbooks("YourWorkbook").Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(x)
        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 x
End Sub
 
Upvote 0
Ah. I wrote it to work in the Workbook with the data in it. This small change should work. Replace "YourWorkbook" with Workbook Name.
VBA Code:
Sub test()

   Dim i As Long, x As Integer
    Dim sht As Worksheet
   
    For x = 1 To Workbooks("YourWorkbook").Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(x)
        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 x
End Sub
Thank you again for your time. I am getting an error. I did try to replace ThisWorkbook with ActiveWorkbook and also the name of the workbook in " " with no luck once I received the error.
1677965941694.png

1677965952327.png
 
Upvote 0
Thank you again for your time. I am getting an error. I did try to replace ThisWorkbook with ActiveWorkbook and also the name of the workbook in " " with no luck once I received the error.
View attachment 86762
View attachment 86763
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)
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

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