Replace certain number with empty cell in a range

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I have a range Range("F14:R40") where each cell is a formula that returns a value.
I'd like to replace the content of each cell that returns zero to empty cell.
So I was thinking somethnig like that would do the trick but unfortunatelly it doesn't...

Code:
With Range("F14:R40")
    .Replace "0", "", xlWhole
End With
or
Code:
With Range("F14:R40")
    .Replace 0, "", xlWhole
End With


Can you please tell me how to do that quickly (in terms of the code time consumption)?
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Well, mostly because I'd like to approach this problem in a different way :)
 
Upvote 0
Well, you could loop through each cell in the range, check if it has a formula and value zero and, if so, clear contents.

However, assuming all formulas in the range return numerical values, I'd be more inclined to start with the formula & change it like so

=IF(current_formula=0,"",current_formula)

Of course that won't leave the cell empty, just appears that way. But then you could use this to make it really empty
Code:
Sub Replace_Results()
  On Error Resume Next
  Range("F14:R40").SpecialCells(xlFormulas, xlTextValues).ClearContents
  On Error GoTo 0
End Sub
 
Upvote 0
Hi Peter.

Thanks for your reply but before you were kind enough to do that I figured out something like below:
Code:
With Range("S14")
    .Formula = "=SUMPRODUCT(F14:R14,F14:R14)"
End With

Range("S14").AutoFill Destination:=Range("S14:S40"), Type:=xlFillValues

With Range("S14:S40")
    .Value = .Value
    .Replace "#N/A", "DEL", xlWhole
End With

For i = 0 To 26

    If Range("S14").Offset(i, 0) = "DEL" Then
        Range("F14:R14").Offset(i, 0).Value = ""
    ElseIf Range("S14").Offset(i, 0) = 0 Then
        Range("F14:R14").Offset(i, 0).Value = ""
    End If

Next i
Do you think my way is much slower?
 
Upvote 0
Do you think my way is much slower?
It solves a different problem so it isn't comparable. Your original request was to delete cells if the formula returned zero. This code only deletes cells if the whole row of formulas return zero.

However, if it seems fast enough to you & does what you want, then use it. :)
 
Upvote 0
But it leads to the same result :)
Ok, thanks a lot, mate!
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,238
Members
448,555
Latest member
RobertJones1986

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