Correct #REF using vba macro

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
I have instances where a #REF! is displayed.

What is the VBA code to scan a sheet and replace all occurences with a specific value.
I tried the following, to no avail

Code:
Sheets("Emp_Availability").Select
 
Cells.Replace What:="#REF!", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Replace always replaces in Formulas, if the #REF error is the result of a formula, then it figures the replace fails.
 
Upvote 0
I'm assuming the #REF is showing up due to a formula?
Doubt this will work if that is the case, but I tested this with just actual cell values being "#REF":
Code:
Sub FindReplace()
        Sheets("Sheet1").Cells.Replace What:="#REF", Replacement:=""
End Sub
and it worked on my test sheet...
 
Upvote 0
Try the sample below. I believe the Find & Replace commands use the same parameters as the last usage. Your last find may have been "LookIn:<wrong place>"

Gary

Code:
Public Sub Test()

Dim oCell As Range

Set oCell = Cells.Find(What:="#REF!", LookAt:=xlPart, LookIn:=xlValues, SearchOrder:=xlByRows, MatchCase:=False)

If Not oCell Is Nothing Then oCell.Value = ""

End Sub
 
Upvote 0
I guess this works:

Code:
Sub ReplaceRefErrors()
    Dim oCell As Range
    For Each oCell In Worksheets("Emp_Availability").UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors)
        If oCell.Value = CVErr(xlErrRef) Then
            oCell.ClearContents
        End If
    Next
End Sub
 
Upvote 0
I have instances where a #REF! is displayed.

What is the VBA code to scan a sheet and replace all occurences with a specific value.
I tried the following, to no avail

Code:
Sheets("Emp_Availability").Select
 
Cells.Replace What:="#REF!", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False

Thanks

Does changing to xlWhole help?

Code:
Sheets("Emp_Availability").Select
 
Cells.Replace What:="#REF!", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False
 
Upvote 0
A change to xlWHole will not help if the error is the result of a formula, the Replace command *ALWAYS* works on formulas or constants, NEVER on formula results.
 
Upvote 0
A change to xlWHole will not help if the error is the result of a formula, the Replace command *ALWAYS* works on formulas or constants, NEVER on formula results.

I realize that. But the OP never clearified whether it was the result of a formula. Prior code to what was posted may have made the cells values. Therefore xlWhole might be what is needed.
 
Upvote 0
Apologies for not clarifying sooner, the #REF! comes from a formula where the cell reference is invalid.

A sample formula is : =TimeSheet!B9

But the content of B9 maybe inconsistent with the target cell.
I have since updated my macro to verify the FROM cell content.

However, I a still interested in the solution to the original question, since there are a number of other errors can surface, such as #DIV, #NULL etc.

It seems of general help, to be able to remove unexpected error content
 
Upvote 0
Does jkpieterse's code above work? At a quick glance it seems like it would take all formula errors into account... but I could be wrong...
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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