Home » excel » Excel VBA Clear formats when value is 0

Excel VBA Clear formats when value is 0

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have created a report in which there are some cells with null values. When my code applies the number format to null cells, it shows up as 0% for blank values. I added a code that then removes any values and format that show up as 0%, but it takes a really long time to run (30 sec or so) and sometimes crashes Excel.

Code:

Application.ScreenUpdating = False

Dim cell As Range
For Each cell In Range("v11:ay9000")
    If cell = 0 Then cell.ClearFormats
Next cell

Application.ScreenUpdating = True

I would appreciate any help with the speed of the code.

How to&Answers:

You have an XY problem.

You’re trying to improve the time it takes to repair your initial problem. Instead, you should just set the correct number format in the first place.

Range("v11:ay9000").numberformat = "0%;-0%;;"