Question

I am trying to delete anything in column B that has a blank in it. A1:A10 = {3,1,10,1,1,10,2,2,2,10}. When I run my code I get a error at the delete row line. It's a "runtime 1004 error, no cells found." There clearly should have been some cells found. What is going on? I have seen some people suggest putting an On Error clause around the delete row, but that just results in nothing getting deleted.

Sub test()
Dim Total_Rows As Integer
Dim rng As Range
Total_Rows = 10
Range("B1", "B" & Total_Rows) = "=if(A1=10,"""",1)"
Range("B1", "B" & Total_Rows).Copy
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Set rng = Range("B1", "B" & Total_Rows)
rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
Was it helpful?

Solution 2

Try this:

rng.AutoFilter 1, "="
rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.AutoFilterMode = False '~~> removes the filter

This filter out blanks or zero length strings then deletes it.
Or you can directly filter A1:A10 like this:

Range("A1:A10").AutoFilter 1, 10
Range("A1:A10").SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.AutoFilterMode = False

OTHER TIPS

You could try looping through the range like below

For Each cell In rng     
   If cell.Value = "" Then cell.EntireRow.Delete    
Next cell

instead of finding a blank cell

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top