Determine if formula will return #Ref! error

混江龙づ霸主 提交于 2019-12-24 03:35:21

问题


I am writing an application within Excel that is pulling data from several databases and presenting this data within the worksheets of an Excel 2010 workbook. Some of the data that I'm recalling form the databases are in the form of Excel 2010 formulas (e.g. ='Budget Estimate'!E46).

In the process of populating individual cells in a worksheet from this dataset, I need to be able to evaluate if the formula I'm downloading will generate an #Ref! error in advance of placing the value in the cell. In the above example, placing that value in the cell via VBA works great provided the worksheet it references exist. If it doesn't, I have a way of finding it, but first I need to detect if it will create an error so the end user doesn't have to deal with the dialog boxes that pop up asking for the path to this orphaned worksheet.


回答1:


Sub Tester()

    Dim v
    v = Application.Evaluate("=Sheet999!A1")

    If IsError(v) Then
        Select Case v
            Case CVErr(xlErrRef): Debug.Print "#ref!"
            Case CVErr(xlErrDiv0): Debug.Print "#div by zero!"
            'etc
        End Select
    End If

End Sub

See: http://www.cpearson.com/excel/ReturningErrors.aspx




回答2:


You may test your formula using Evaluate. Try:

If IsError(Evaluate("='Budget Estimate'!E46")) Then
    'other cool stuff here
End If

This will capture Errors before you put it in a cell however not specific to Reference Error.
This will capture all errors on the formula. Not really 100% the answer you're looking for but HTH.



来源:https://stackoverflow.com/questions/28038285/determine-if-formula-will-return-ref-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!