If cell length < 3, delete row. For whole column

squidmark

Board Regular
Joined
Aug 1, 2007
Messages
105
I get spreadsheets where previous users "cleared" a cell by using the spacebar. Now, I need some code that will look at all of column G and delete the rows where the cell in column G is blank. But sometimes the "blank" cell is really a space, so it just looks blank.

I've used this:
columns("G").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

to delete the rows that are actually blank. Which is how I realized many aren't actually blank.

Is there a way to just say if len(g1)<2, delete whole row, but have it look at the entire column all at once as the code above does, rather than using something like a for-next loop that will take more time to run?

Yes, I'm a VBA noobie.

Thanks.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
you could try replacing single spaces with blanks first

Code:
    With Columns("G")
        .Replace what:=" ", replacement:="", lookat:=xlWhole
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With

PS, you might need a second pass with two spaces, just in case
 
Upvote 0
You could use a validation rule like this to stop them just putting spaces in the cells:

=NOT(AND(LEN(A1)>0,LEN(TRIM(A1))=0))

Dom
 
Upvote 0
you could try replacing single spaces with blanks first

Code:
    With Columns("G")
        .Replace what:=" ", replacement:="", lookat:=xlWhole
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With

PS, you might need a second pass with two spaces, just in case

Thanks, Weaver. I used your code, and then I added another line of code for cells with two spaces, but it is still not getting rid of the cells with only a space in them. It's not replacing the spaces with clear.

The whole macro right now is:

Sub DelBlank()
With Columns("G")
.Replace what:=" ", replacement:="", lookat:=xlWhole
.Replace what:=" ", replacement:="", lookat:=xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

I also tried to replace the spaces with "asdf" just to see if the replacement was taking place -- I changed: replacement:="" to replacement:="asdf". Is there another way to replace a space with a blank?
 
Upvote 0
That should work fine although the line you've added to get rid of two spaces only has one in it.

Dom
 
Upvote 0
Thanks, Weaver. I used your code, and then I added another line of code for cells with two spaces, but it is still not getting rid of the cells with only a space in them. It's not replacing the spaces with clear.

The whole macro right now is:

Sub DelBlank()
With Columns("G")
.Replace what:=" ", replacement:="", lookat:=xlWhole
.Replace what:=" ", replacement:="", lookat:=xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

I also tried to replace the spaces with "asdf" just to see if the replacement was taking place -- I changed: replacement:="" to replacement:="asdf". Is there another way to replace a space with a blank?

Is it possible there are non printing characters in there? This worked fine when I tried it on some test data? Maybe the space is CHAR 160, although I can't see how.

In a separate column, use =Len(G2) to find the 1 character 'empty' cells or =code(G2) to find out if it's 32 (space) or something else.
 
Upvote 0
That should work fine although the line you've added to get rid of two spaces only has one in it.

Dom
Domski, I think that might be down to the lack of code tags - html doesn't like double spaces.
 
Upvote 0
Is it possible there are non printing characters in there? This worked fine when I tried it on some test data? Maybe the space is CHAR 160, although I can't see how.

In a separate column, use =Len(G2) to find the 1 character 'empty' cells or =code(G2) to find out if it's 32 (space) or something else.

It's turning up with code 160. What is that?
 
Upvote 0
It's turning up with code 160. What is that?
It's an 'alternate' code for a space, although I don't know much more than that.

See if this works:

Code:
    With Columns("G")
        .Replace what:=Chr(160), replacement:="", lookat:=xlWhole
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

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