Help (!) with Range.Precedents

Indystick

Board Regular
Joined
Mar 2, 2018
Messages
60
Hi all,

I have a sub that works correctly using Range.Precedents, but when I try and pass a range to it from a function, returns the incorrect results. Here is a simplified version of what I am trying to do:

Imagine in cell A3 I have the formula =A2 + A2 and I have the following sub in ThisWorkbook:

Code:
Sub MySub()    
    Dim myRange As Range
    Dim rngPrecedents As Range
    Dim rngPrecedent As Range
    
    Set myRange = Worksheets("Sheet1").Range("A3")
    Set rngPrecedents = myRange.Precedents
    
    For Each rngPrecedent In rngPrecedents
        Debug.Print rngPrecedent.Address
    Next
        
    
End Sub

The sub will print $A$1 AND $A$2 in the immediate window when it is run. So far so good.

Now imagine, I create function that receives a cell reference and passes it as a range to the sub I created above (with a minor modification):
Code:
Function FORMULATEST(testCell As Range) As Boolean    Dim cell As Range
    Set cell = testCell
    Call ThisWorkbook.MySub(cell)
    FORMULATEST = True
End Function

and I have modified my sub to receive the range captured by the function:

Code:
Public Sub MySub(cellAddress As Range)    Dim myRange As Range
    Dim rngPrecedents As Range
    Dim rngPrecedent As Range
    
    Set myRange = cellAddress
    Set rngPrecedents = myRange.Precedents
    
    For Each rngPrecedent In rngPrecedents
        Debug.Print rngPrecedent.Address
    Next
        
    
End Sub

Now the Debug.Print prints the range passed to the sub, not the cell references of the formula of that range. In other words, if I put =FORMULATEST(A3) in a cell, the sub prints $A$3, not $A$1 and $A$2 as it should.

I, for the life of me, cannot figure out what the problem is. It seems like it should work.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Thank you Marcelo, that was good information. Can you think of a workaround?
 
Last edited:
Upvote 0
Right, which is why I used the precedents in MySub.

My problem is, I need to tell the Sub what cell to get the precedents, and then I am going to evaluate them and return a Boolean to the worksheet; which I don’t think I can do from a Sub.
 
Last edited:
Upvote 0
Maybe you can select the cell and in the sub use something like this

Code:
Dim bResult as Boolean

With Selection
   'code
   'code
   Range("A1") = bResult 'for example
End With

M.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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