Replace certain number with empty cell in a range

DonAndress

Active Member
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Why not just have it in the formula?

Well, mostly because I'd like to approach this problem in a different way

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``````

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?

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.

But it leads to the same result
Ok, thanks a lot, mate!

But it leads to the same result
It might on your worksheet, but it didn't on mine.
Anyway, if you are happy, I'm happy.

Replies
1
Views
304
Replies
1
Views
157
Replies
10
Views
4K
Replies
3
Views
311
Replies
2
Views
245

Forum statistics

1,221,052
Messages
6,157,632
Members
451,426
Latest member
VinnyDoesntKnowExcelCode

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.

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

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