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:
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):
and I have modified my sub to receive the range captured by the function:
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.
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.