VBA - remove formatting from many blank cells

JustDude

New Member
Joined
Jul 30, 2013
Messages
14
Hi everyone,

I have data in over 200 000 rows, accross a couple columns (A:I). Some cells are filled in, some are blank - but all have certain formatting.
Now, I want to clear the formatting from blank cells only, starting from second row.
I need to do it the fastest way possible, but cannot sort the data. So I wrote this code:

Code:
Sub test()

ActiveSheet.Range("A2:I" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeBlanks).ClearFormats

End Sub

Unfortunately, the code doesn't work as expected - it removes formatting from ALL cells, instead of blank cells only. And it takes over 2 minutes to complete that (way too long!)
Can you help me out with that? What would be the best (fastest!) way to do that correctly?

Thank you very much in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
What can you do with this code?

Code:
Sub Foo()
With ActiveSheet.UsedRange
    .SpecialCells(xlCellTypeBlanks).ClearFormats
End With
End Sub
 
Upvote 0
Don't need With / End with

Code:
Sub Foo()
ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
End Sub
 
Upvote 0
It is really odd that it works on everything except the font. If I set the used range font to bold and run the macro, it removes any colors and borders but only the first empty cell changes the font to normal. The rest will revert to bold.
 
Upvote 0
What can you do with this code?

Code:
Sub Foo()
With ActiveSheet.UsedRange
    .SpecialCells(xlCellTypeBlanks).ClearFormats
End With
End Sub

Don't need With / End with

Code:
Sub Foo()
ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats
End Sub

Jim, Michael,
Thank you, but this still doesn't work :(
Same issues, it removes formatting from ALL of the cells, instead of blanks only - which is what I need. And it takes 2 minutes to complete that.

Is there any way to do that, as I described in my first post?

Thank you in advance.
 
Upvote 0
With your subject sheet the activesheet go to VBE window and in the immediate window enter:

? Activesheet.Usedrange.address (and press enter)
What do you get? Is it covering the desired worksheet area?

While still in the immediate window now enter:
Activesheet.Usedrange.specialcells(xlcelltypeBlanks).select (and press enter) Note the EXCLUSION of the "?" at the beginning of the code.

Look NOW at your activesheet -- ONLY your BLANK CELLS SHOULD BE SELECTED? Is this the case??
 
Upvote 0
Jim, Michael,
Thank you, but this still doesn't work :(
Same issues, it removes formatting from ALL of the cells, instead of blanks only - which is what I need. And it takes 2 minutes to complete that.

Is there any way to do that, as I described in my first post?

Thank you in advance.

Try adding this line before the clearformats line:
Code:
Application.Calculation = xlCalculationManual
and this one after the clearformats line:
Code:
Application.Calculation = xlCalculationAutomatic

That should speed up the process. But still confounded by the response to the SpecialCells method.
 
Upvote 0
With your subject sheet the activesheet go to VBE window and in the immediate window enter:

? Activesheet.Usedrange.address (and press enter)
What do you get? Is it covering the desired worksheet area?

It covers the used range area, and I get: $A$1:$I$208274

While still in the immediate window now enter:
Activesheet.Usedrange.specialcells(xlcelltypeBlanks).select (and press enter) Note the EXCLUSION of the "?" at the beginning of the code.
Look NOW at your activesheet -- ONLY your BLANK CELLS SHOULD BE SELECTED? Is this the case??

No, once I type: Activesheet.Usedrange.specialcells(xlcelltypeBlanks).select and press Enter, the worksheet "freezes" for about 2 minutes, and after that time it shows ALL cells selected, not the blank cells.

It's a huge file, see for yourself: Zippyshare.com
 
Upvote 0
And you are trying to clear over a million cells.
I think you may have to do a loop through rather than a mass edit !!
 
Upvote 0
And you are trying to clear over a million cells.
I think you may have to do a loop through rather than a mass edit !!

Michael,
But doing a loop through such a range takes much longer than 2 minutes!
Long time is just a side issue. The biggest issue here is that .SpecialCells(xlcellTypeBlanks) doesn't do its job - it removes formatting from ALL cells (instead of blanks only), as expected.

 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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