简体   繁体   中英

Can I find whether a Cell in Excel is referenced in a formula?

I have searched alot of sites on the internet, and I'm guessing I haven't found an answer because it is not possible, to try to understand the following:

I would like to understand if a Cell containing a value (say 10) plays into / forms part of a formula further into the document. I KNOW that the cell I have is NOT a formula cell, it is a single entry, however, I am trying to understand if this cell forms part of a formula. The need for this is there are ALOT of formulas and ALOT of data, and I am trying to see which data is relvant and which data is not.

Hope somone can help?

Kind regards

Paul

Manually you can use "Trace Dependents" on the 'Formula Auditing' menu under "Formulas" tool tab.

Using VBA you can check the number of dependents like this:

Range("A1").Dependents.Count

If you want a list of cell addresses that cell A1 is used in you can do this:

MsgBox (Range("A1").Dependents.Address)

I have A1 used in two other cells so my result looks like this:

在此处输入图片说明


Full code with error checking:

Sub test()
    On Error Resume Next
    If Range("A1").Dependents Is Nothing Then
        MsgBox ("No dependents found")
    Else
        MsgBox (Range("A1").Dependents.Address)
    End If
End Sub

The Dependents property of a Range object gives you the dependent cells of your formula. Be careful you will get an error if there are no dependent cells

Function hasDependents(r As Range) As Boolean
    On Error GoTo err
    hasDenpedents = r.Dependents
    Exit Function
err:
    hasDependents = False
End Function

And then

if hasDependents([A1]) then
    '...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM