VBA - Find, Replace #NAME Errors with Formula Value

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
Can someone help me with a function that would evaluate the contents of a sheet, find any #NAME errors, and replace contents with " ' " + contents to show the value (rather than the error)?

Here's start, but it's more of an algorithm than functional code. Thoughts here?
Code:
Dim sh As Worksheet
Dim fnd As Variant
Dim rpl As Variant

fnd = CVErr(ActiveWorkbook.Worksheets.sh.Cells)
rpl = "'" & ActiveWorkbook.Worksheets.sh.Cells.Value

For Each sh In ActiveWorkbook.Worksheets
  sht.Cells.Replace what:=fnd, Replacement:=rpl, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
Next sh
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If you're getting a #NAME error, it means you are trying to reference a function or named range that does not exist and Excel can't evaluate it.
Can you give an example of something that is causing this error?
 
Upvote 0
Not sure why, but perhaps something like:

Code:
For Each sh In Worksheets
    On Error Resume Next
    Set rngErrors = sh.Cells.SpecialCells(xlFormulas, xlErrors)
    On Error GoTo 0
    If Not rngErrors Is Nothing Then
        For Each rng In rngErrors
            If rng.Value = CVErr(xlErrName) Then
                rng.Value = "'" & rng.Formula
                rng.Interior.Color = vbRed
            End If
        Next rng
    End If
Next sh
 
Upvote 0
To answer why, I'm dealing with a report that's auto-generated by a piece of software. The report includes a repeated format/template for multiple items. When put to Excel, the text-to-columns isn't always correct. So for some of the items, the name of the item gets split across more than one column.

I'm creating a macro that will go in and cleanup the format and make it more user friendly. Some of the item names include dashes (-). So in one specific example, a cell value became: "=-ALT" (which resulted in the #NAME error). I assume Excel added the equal sign on it's own. I'd still like to preserve that piece of the item name, so my idea was to just add "'" before, and then I can concatenate the item name back together.

Maybe this isn't the right approach. I'd appreciate any additional thoughts. Thanks again
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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