Vba byref link is not available
Given that the called procedure actually sets the passed Range ByRef object, I expect the object reference to be available in the calling procedure. But that's still nothing.
What is a fix?
call
Private Sub Specs_TryGetRangeFromDefinedName_CanFindGlobalName()
Dim Specs As New SpecSuite
Dim rngResult As Excel.range
Dim bResult As Boolean
bResult = TryGetRangeFromDefinedName(rngResult, "LongTermTaxRate", ThisWorkbook.Name)
With Specs.It("should return refersTo range when the name is global")
.Expect(bResult).ToEqual True
.Expect(rngResult.address).ToEqual "$B$2" ** FAIL (object is Nothing)2
End With
SpecRunner.RunSuite Specs
End Sub
called
Public Function TryGetRangeFromDefinedName(ByRef aRange As Excel.range, _
ByRef aName As String, _
ByRef aWkbName As String, _
Optional ByRef aSheetName As String = vbNullString) As Boolean
Dim rngResult As Excel.range
If IsValued(aName) And IsValued(aWkbName) Then
On Error Resume Next
If IsValued(aSheetName) Then
' local name (presumably)
Set rngResult = Workbooks(aWkbName).Worksheets(aSheetName).range(aName)
Else
' global name (presumably)
Set rngResult = Workbooks(aWkbName).Names(aName).RefersToRange
End If
TryGetRangeFromDefinedName = (err.Number = 0)
On Error GoTo 0
End If
End Function
source to share
The parameter is aRange
never used, but rngResult
assigned but never referenced. Get rid of rngResult
and assign aRange
.
Also, the purpose of your method would be clearer if parameters were passed String
ByVal
as they are not assigned.
All these observations are Rubberduck code review results (disclaimer: I am a co-owner of this project).
source to share