Clear cell in range if contains text

tls123

New Member
Joined
Sep 23, 2014
Messages
7
I'm working on an Excel 2013 macro that calculates values from another sheet within a workbook. If there are missing data in the first sheet, my calculations return "#Value!". This text screws up the next round of calculations so I was trying to figure out how to clear cells that contain "#Value!". I recorded the following by running a Find/Replace with "". It seemed to work until I ran it against a sheet that did not have any cells with "#Value!".

Range(Cells(2, 3).Address, Cells(42,8).Address).Select
Selection.Find(What:="#Value!", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Replace What:="#Value!", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

I then tried to include an If/Then statement:

If Range(Cells(2, 3).Address, Cells(42,8).Address).Value = "#Value!" Then
'find/replace code above'
End If

...but I get a Run Time Error 13 Type Mismatch. Please help.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
either the excel function =iferror(value,value if error) so i.e =iferror(0/0,"") this causes an error so displays nothing

Or you can reference it in vba. Excel.WorksheetFunction.IfError Range("A1"), ""

you could also say presuming you are looking for a number

if vba.isnumeric(Range("A1").value) = false then

range("a1").value = ""

end if
 
Upvote 0
That doesn't seem to help.

I want to search all cells within a range, find those with "#Value!", and then clear those cells. I can't seem to apply your code to mine.
 
Upvote 0
try this

Code:
[COLOR=#333333]If excel.worksheetfunction.iserror(Range(Cells(2, 3).Address, Cells(42,8).Address).Value) = true then
[/COLOR]
[I]
End If[/I]
 
Upvote 0
Or I'll just figure out a solution on my own...


Dim ValRange As Range
Dim cValue As Range

Set ValRange = Range(Cells(2, 3), Cells(42,8)
For Each cValue In ValRange
If Not Application.IsNumber(cValue) Then cValue.ClearContents
Next cValue
 
Upvote 0
But congratulations i'm glad you got to where you wanted to be.

It would be better to stop the errors happening in the first place tbh.
 
Upvote 0
Or I'll just figure out a solution on my own...


Dim ValRange As Range
Dim cValue As Range

Set ValRange = Range(Cells(2, 3), Cells(42,8)
For Each cValue In ValRange
If Not Application.IsNumber(cValue) Then cValue.ClearContents
Next cValue

Either one of these single lines of code (no loop needed) should work; however, which one depends on if the #VALUE! error is a text constant or the result of a formula, so try each one and the one that works is the one that you should use. So, either this one...

Range("C2:H42").SpecialCells(xlFormulas, xlErrors).ClearContents

or this one...

Range("C2:H42").SpecialCells(xlConstants, xlErrors).ClearContents
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,326
Members
448,564
Latest member
ED38

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