1
votes

In Microsoft Excel I have a cell that generates a #REF! error (because it refers to a worksheet that's been deleted). I want to write VBA code that will retrieve the formula that was in that cell. Has the REF error wiped out the original formula so it's lost forever? I know that IsError(ActiveCell.Value) will tell me whether a cell has an error, and I know that ActiveCell.Value will give me the error number (2023 for #REF), but that still leaves me without the original formula. If I had the actual formula, I could change the sheet name and put it back in the troublesome cell.

2
The #REF! part should only have wiped out the sheet name. If you want to use a different sheet name, just replace #REF! with whatever that sheet name is.Rory

2 Answers

3
votes

Has the #REF! error wiped out the original formula so it's lost forever?

Yes! You cannot reverse it.


However, if you are looking for an easy way, to keep the formulas, before deleting workbook that they refer to, you may add a ' sign in front of each formula. Thus, the formulas would be saved as text and you would avoid #REF!:

Public Sub AllFormulasToText()

    Dim myCell As Range

    For Each myCell In ActiveSheet.UsedRange
        If myCell.HasFormula Then myCell.Value = "'" & myCell.Formula
    Next myCell

End Sub
1
votes
Check = Range("A1").Formula 'Reference to the cell your formula is in