Function returns Range --> error 91

D

djlewis2

This function...

Function Range4(R1 As Integer, C1 As Integer, R2 As Integer, C2 As
Integer) As Range
Set Range4 = Range(Cells(R1, C1), Cells(R2, C2))
End Function

.... works fine through the assignment to Range4 (gets the correct
range), but hangs at the End Funciton line with
Run-time error '91':
Object variable or With block variable not set

What am I doing wrong?

I've tried: adding "ActiveSheet." to everything; an auxilliary Range
variable; searching this group.

Thanks. --David.

PS: Anybody know a better way to construct a simple rectangular range
using numeric arguments rather than string addresses. But I'd still
like to know why this function fails. It fails even if I assign a very
simple range to Range4, like ActiveCell.
 
B

Bob Phillips

When I called it using

Set r = Range4(1, 2, 3, 4)

it worked fine. What values are you passing?

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
T

Tom Ogilvy

In a general module, this worked fine for me:

Function Range4(R1 As Integer, C1 As Integer, R2 As Integer, C2 As Integer)
As Range
Set Range4 = Range(Cells(R1, C1), Cells(R2, C2))
End Function

Sub Tester3()
Dim i1 As Integer, i2 As Integer
Dim j1 As Integer, j2 As Integer
i1 = 10
i2 = 20
j1 = 5
j2 = 26
MsgBox Range4(i1, j1, i2, j2).Address
End Sub
 
D

David Lewis

Hmmm... Well, I tried this on an entirely blank workbook, immediately
aftr rebooting my system...

Function Range4(R1 As Integer, C1 As Integer, R2 As Integer, C2 As
Integer) As Range
Set Range4 = ActiveCell
End Function

Sub SetupStuff()
Dim R As Range
R = Range4(1, 2, 3, 5)
End Sub

.... and it still fails the same way. Obviously something is broken.
Good news -- I can still program VBA. Bad news -- I'm stuck, and
wondering what else what might go wrong?

Any ideas? I'm using: VB 6.3; Excel 2002 SP3; WinXP Pro SP1; on a Dell
Latitude.
 
C

Chip Pearson

In your test code, you need to change
R = Range4(1, 2, 3, 5)
to
Set R = Range4(1, 2, 3, 5)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top