VBA code to delete formula if result = zero

asyouread

New Member
Joined
Nov 25, 2015
Messages
5
I'm trying to reduce an Excel 2010 file in size by clearing the contents of cells where a formula has returned a zero value. I have found this VBA script which does the job but not very well for the data set I'm working with:

Sub delzero()
For Each Cell In [W12:BF24459]
If Cell.Value = "0" Then Cell.ClearContents
Next Cell
End Sub

It will work for one column e.g.[W12:W24459], although very slowly but I need to do the whole data set [W12:BF24459}.

Does anyone have some VBA code to do this quickly and without crashing the file?

Thanks in advance
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I've tried it on a blank sheet, and it ran in seconds - so the delay is not because of the number of cells being processed.

Have you tried turning recalculation off before running it? Otherwise every time that the macro is blanking a cell, it will be recalculating the entire workbook - which will slow it down.

Another thought is to turn off screen updating while its running - to do this add: Application.ScreenUpdating = False before the For... line, and Application.ScreenUpdating = True after the Next... line.
 
Upvote 0
try this, see if it works any faster


Sub DelZero()

Range("W12:BF24459").Select
Cells.Replace What:="0", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
 
Upvote 0
Dryver14, that code deleted all of the zeros, not just those in the range. I'm not sure why it would look outside of the range specified?
 
Upvote 0
Thanks Trevor_S, I added the Application.ScreenUpdating lines but it hasn't made much of a difference to the running speed (or stability).

Perhaps the issue is with the size of the formula and the number of cells it's running on...here is the formula which is phasing sales revenue based on contract dates: =IF(AND($H12<=W$2,$I12>=W$2),($L12/($I12-$H12+1)*(W$2-$H12+1))-SUM(V12:$V12),IF($I12<=W$2,$L12-SUM($V12:V12),0)). This formula is located in 23,326 rows across 36 columns (being the monthly time series).

Any suggestions?

Thanks
 
Upvote 0
I'm not sure why it would look outside of the range specified?
Because of ..

Sub DelZero()

Range("W12:BF24459").Select
Cells.Replace What:="0", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
.. but I don't think that will solve your problem anyway because your zeros are the results of formulas.
 
Upvote 0
Not sure how it will go for speed with your large range and formulas, but you could try this in a copy of your workbook.

Rich (BB code):
Sub DelZeros()
  Dim f, v
  Dim i As Long, j As Long
  
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  With Range("W12:BF24459")
    f = .Formula
    v = .Value
    For i = 1 To UBound(v, 1)
      For j = 1 To UBound(v, 2)
        If v(i, j) = 0 Then
          If Left(f(i, j), 1) = "=" Then
            f(i, j) = vbNullString
          End If
        End If
      Next j
    Next i
    .Value = f
  End With
  Application.Calculation = xlAutomatic
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Stupid suggestion, but what the heck:

do you still need the formulae once they have been calculated? In other words: do you change values in the sheet once the zeroes are gone to update formula output?
If not, you might as well throw out all formulae and for an update, simply start from a base-file which has all the formulae intact.

But as I said: I don't know how you intend to work with it.
 
Upvote 0
Yes I still need the formula as I it is a forecast model with variable inputs. The intention is to re-calculate and copy formulae across the entire series if variables are changed. I have another macro that makes this process fairly quick.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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