The Problem (Q-score 15, ranked #21st of 303 in the Excel VBA archive)
The scenario as originally posted in 2009
I seem to be getting a type mismatch error when trying to do something like this:
In new workbook:
A1 B1
5 4
Function Test1() As Integer
Dim rg As Range
Set rg = Test2()
Test1 = rg.Cells(1, 1).Value
End Function
Function Test2() As Range
Dim rg As Range
Set rg = Range("A1:B1")
Test2 = rg
End Function
Adding =Test1() should return 5 but the code seems to terminate when returning a range from test2(). Is it possible to return a range?
Why this Range / Worksheet targeting trips people up
The question centers on reaching a specific cell, range, or workbook object. In Excel VBA, this is the #1 source of failures after activation events: every property (.Value, .Formula, .Address) behaves differently depending on whether the parent Workbook is explicit or implicit.
The Verified Solution — elite answer (top 10 %%) (+36)
12-line Excel VBA pattern (copy-ready)
A range is an object. Assigning objects requires the use of the SET keyword, and looks like you forgot one in your Test2 function:
Function Test1() As Integer
Dim rg As Range
Set rg = Test2()
Test1 = rg.Cells(1, 1).Value
End Function
Function Test2() As Range
Dim rg As Range
Set rg = Range("A1:B1")
Set Test2 = rg '<-- Don't forget the SET here'
End Function
When to Use It — vintage (14+ years old, pre-2013)
Ranked #21st in its category — specialized fit
This pattern sits in the 89% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.
What changed between 2009 and 2026
The answer is 17 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.