How can a VBA macro find which cells are being referenced in a formula

phg

Board Regular
Joined
Jul 4, 2007
Messages
81
I have a spreadsheet with a number of different formulae on it. I have a macro which I want to extract which cells are referenced in the formula in the active cell and then branch based on the which cell is referenced.

Is there a way to find the referenced cells without parsing the formula string?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Have a look at the Precedents and DirectPrecedents properties.
 
Upvote 0
Thanks for the reply.

I had a look at the Precedents and DirectPrecedents properties but for a formula referencing three cells it only gave two of them.

Am I missing something or are they nested in some way?
 
Upvote 0
Hi there,

I am not that expert but try the following:

If you want to trace the dependents and precedents then:

First make sure that Tools/Options/View/Objects Hide All option button is NOT selected.

Then run this code in the sheet module that you want to show the precedents and dependents.


Sub ShowRelationships()
Dim c As Range

For Each c In Range("A1:J10") ‘change To your range
c.ShowDependents
c.ShowPrecedents
Next c
End Sub



Hope that will help .
 
Upvote 0
Thanks for the reply.

I had a look at the Precedents and DirectPrecedents properties but for a formula referencing three cells it only gave two of them.

Am I missing something or are they nested in some way?

It might have something to do with the way you are examining the precedents. Can you show us your code?
 
Upvote 0
I have actually just been looking at ActiveCell in the watch window with a sample formulae.

If th formula is

=E13/F13/E14

ActiveCell.DirectPrecedents.Count = 3 so it looks like it knows there are 3 cells involved

However
ActiveCell.DirectPrecedents.Areas.Count = 2 so it seems to have grouped the cells into two groups

ActiveCell.DirectPrecedents.Areas(1).Cells.Count = 1
ActiveCell.DirectPrecedents.Areas(2).Cells.Count = 2

so
ActiveCell.DirectPrecedents.Areas(1).Cells(1).Address = "$F$13"
ActiveCell.DirectPrecedents.Areas(2).Cells(1).Address = "$E$13"
ActiveCell.DirectPrecedents.Areas(2).Cells(2).Address = "$F$14" seems to be all the referenced cells but I was hoping there was an easier way and it also doesn't know anything about the formula - i.e. whether it was defined as F12:F14 or F12,F13, F14

Thanks for your interest and help
 
Upvote 0
To deal with precedent cells that are on a different sheet, the .NavigateArrow method needs to be used.
 
Upvote 0
This UDF will return a string describing the number and location of the dependents of the input cell. Setting the doPrecidents argument to True will return the precedents of the cell.

Since .NavigateArrow changes the ActiveCell, this UDF returns an error when called from a cell's formula.

Code:
Function oneCellsDependents(ByVal inRange As Range, Optional doPrecedents As Boolean) As String
Dim inAddress As String, returnSelection As Range
Dim i As Long, pCount As Long
Rem remember selection
Set returnSelection = Selection
inAddress = fullAddress(inRange)

With inRange
    .ShowPrecedents
    .ShowDependents
    .NavigateArrow doPrecedents, 1
    Do Until fullAddress(ActiveCell) = inAddress
        pCount = pCount + 1
        .NavigateArrow doPrecedents, pCount
        oneCellsDependents = oneCellsDependents & fullAddress(Selection) & Chr(13)
        .NavigateArrow doPrecedents, pCount + 1
    Loop
    oneCellsDependents = inAddress & " has " & pCount _
                        & IIf(doPrecedents, " precedent cells.", " dependent cells.") & vbCrLf & oneCellsDependents
    .Parent.ClearArrows
End With

Rem return selection to where it was
With returnSelection
    .Parent.Activate
    .Select
End With

End Function

Function fullAddress(inRange As Range) As String
With inRange
    fullAddress = .Parent.Name & "!" & .Address
End With
End Function
It will not catch dependents or precedents that are in a different workbook.
 
Last edited:
Upvote 0
Hello,

Thank you Mickericson. The function does not work exactly when the dependent or precedent are on the other sheets. Here is a modification. Is it correct?

Code:
Function oneCellsDependents(ByVal inRange As Range, Optional doPrecedents As Boolean) As String
Dim inAddress As String, returnSelection As Range
Dim i As Long, pCount As Long, qCount As Long
Rem remember selection
Set returnSelection = Selection
inAddress = fullAddress(inRange)

Application.ScreenUpdating = False
With inRange
    .ShowPrecedents
    .ShowDependents
    .NavigateArrow doPrecedents, 1
    Do Until fullAddress(ActiveCell) = inAddress
        pCount = pCount + 1
        .NavigateArrow doPrecedents, pCount
        If ActiveSheet.Name <> returnSelection.Parent.Name Then
            Do
                qCount = qCount + 1
                .NavigateArrow doPrecedents, pCount, qCount
                oneCellsDependents = oneCellsDependents & fullAddress(Selection) & Chr(13)
                On Error Resume Next
                .NavigateArrow doPrecedents, pCount, qCount + 1
            Loop Until Err.Number <> 0
            .NavigateArrow doPrecedents, pCount + 1
        Else
            oneCellsDependents = oneCellsDependents & fullAddress(Selection) & Chr(13)
            .NavigateArrow doPrecedents, pCount + 1
        End If
    Loop
    oneCellsDependents = inAddress & " has " & pCount + Application.WorksheetFunction.Max(0, qCount - 1) _
                        & IIf(doPrecedents, ' precedent range(s).', ' dependent range(s).') & vbCrLf & oneCellsDependents
    .Parent.ClearArrows
End With

Rem return selection to where it was
With returnSelection
    .Parent.Activate
    .Select
End With

End Function
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
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