Need help with deleting rows

hjason315

New Member
Joined
Jul 11, 2011
Messages
19
What I have are two columns A and B and for instant, some cells from row 1-50 are filled with numbers. I'd like to delete all rows in which both cells of column A and B are empty in those rows.
The code I found below only find empty cells in Column A then delete the entire rows. However, if cell in Column B is filled but empty in Column A, that row also gets deleted. (which I don't want this to happen)
Could anyone help me improve this code so that only when both cells in A and B are empty, the rows get deleted. Other rows remain the same if either cell of column A and B is filled.

Thanks in advance!




Sub deleteblankrows()


Application.ScreenUpdating = True
[a:a].AutoFilter Field:=1, Criteria1:="="
[a2:a65536].SpecialCells(xlVisible).EntireRow.Delete
If [a1] = "" Then [1:1].Delete
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Perhaps:

If [a1] = "" And [b1] = "" Then [1:1].Delete

Alternatively:
Code:
Sub delrows()
Dim i As Long
Application.ScreenUpdating = False
For i = 50 To 2 Step -1
    If Cells(i, 1).Value = "" And Cells(i, 2).Value = "" Then _
        Cells(i, 1).EntireRow.Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thank a lot, your code is amazing.
But something's still not working right with adding [b1]
actually the cells I am working on are in column J and K, and I've changed everything A and B to J and K, is there anything else I should do about it? because adding If [a1] = "" And [b1] = "" Then [1:1].Delete
makes no difference. Thanks again!
 
Upvote 0
Cells(i, "J").value or Cells(i, 10).value

to refer to column j.

You should be able to figure the rest.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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