Creating a Macro/Script where it automatically deletes All Rows With a Blank Cell

deejay1234

New Member
Joined
Oct 2, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to create a macro or a script where it automatically deletes all rows that has a blank cell. When recording the script on Excel it recognizes the steps that I am doing but when I use it on a different sheet it doesnt do it properly. Please help.

NameLocationSale
AaronUSA210
BiancaCanada
Calvin310

From the table above, I want to remove the 2nd and 3rd rows since it has a blank cell. So in this example when I run the Macro or the script it should only return as the 1st row.

Thanks,
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Which columns exactly do you want to check?
Please provide the letters (is it just columns A-C, or are there more than that)?
 
Upvote 0
If it is only columns A-C, then this code should work:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False

'   Find last row on sheet with data
    lr = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       If all columns are not populated, delete row
        If Application.WorksheetFunction.CountA(Range(Cells(r, "A"), Cells(r, "C"))) < 3 Then Rows(r).Delete
    Next r

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
If it is only columns A-C, then this code should work:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False

'   Find last row on sheet with data
    lr = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       If all columns are not populated, delete row
        If Application.WorksheetFunction.CountA(Range(Cells(r, "A"), Cells(r, "C"))) < 3 Then Rows(r).Delete
    Next r

    Application.ScreenUpdating = True
   
End Sub
Hi Joe,

What if the Columns got bigger to say A-L , should I just then change this "If Application.WorksheetFunction.CountA(Range(Cells(r, "A"), Cells(r, "C"))) < 3 Then Rows(r).Delete" to
"If Application.WorksheetFunction.CountA(Range(Cells(r, "A"), Cells(r, "L"))) < 3 Then Rows(r).Delete"

THanks?
 
Upvote 0
You would need to change the "C" to an "L", and change the 3 to 12, as that means that there are 12 columns, not 3.
Basically, we are counting the number of populated cells in column A:L, and want to see if it is less than 12.
If it is, it means that there is at least one blank column.
 
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,099
Members
452,612
Latest member
MESTeacher

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