VBA Error on Logic to Remove Rows based on Blank Cells

jay_hl

New Member
Joined
Jun 28, 2012
Messages
27
Hello

I have some VBA which removes a row, based on the cells in a selected column being blank. Its quite powerful, so wanted to add a check that I don’t select an entirely blank column, where all the rows would be deleted (ie the whole sheet).

I therefore wanted to count how many rows it was expecting to remove, and if that totaled all rows in the Sheet, give me a Yes/No warning box. However I'm getting an error.

Can anyone tweak my code to work, including for an entirely blank column.

Thanks. Jay

----
Sub Macro1()

SelectedColumn = ActiveCell.Column
If Columns(SelectedColumn).SpecialCells(xlCellTypeBlanks).CountA = 1048576 Then

MsgBox "You are about to remove all rows in the sheet." & vbCrLf & vbCrLf & "Are you sure you have selected the correct column?", vbYesNo, "Are you sure?"

If vbYes Then

Columns(SelectedColumn).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

Else
Exit Sub
End If

Else

Columns(SelectedColumn).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End If

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
When you step through the code what line causes the error?
 
Upvote 0
This one
Columns(SelectedColumn).SpecialCells(xlCellTypeBlanks).CountA

Ive also tried, but no luck
Columns(SelectedColumn).SpecialCells(xlCellTypeBlanks).Cells.CountA
 
Upvote 0
Hello, is anyone able to help tweak the VBA I posted, as I still haven’t managed to crack it yet. Thanks.
 
Upvote 0
1) CountA doesn't exist as VBA property.
2) The speciall cells function seems to be aware of the used range and as such will never return 1048576 unless you have something entered on that row which seems unlikely.
It only counts blanks down to the last row with something in it or has been formatted
The code below checks if the number of blank cells in the column = the number of rows from row 1 to the last used row, which would mean there is no data in that column.

VBA Code:
Sub DeleteBlankCellsInColumn()
    Dim SelectedColumn As Long
    SelectedColumn = ActiveCell.Column
    ActiveSheet.UsedRange                       ' Ensure used range is up to date
    With Columns(SelectedColumn)
        ' Check if blank cells in column = all rows in sheet
        If .SpecialCells(xlCellTypeBlanks).Count = .SpecialCells(xlCellTypeLastCell).Row Then
            MsgBox "You are about to remove all rows in the sheet." & vbCrLf & vbCrLf & "Are you sure you have selected the correct column?", vbYesNo, "Are you sure?"
            If vbYes Then
                .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Else
                Exit Sub
            End If
        Else
             .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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