PDA

View Full Version : Solved: Setting a Range as a Class Property



DRJD
08-19-2008, 07:04 AM
Hi,

I'm trying to code a class at the moment and I want to set a range as one of it's properties. Is this going to be possible? I've included a snippet of the code that I can't get to work. Can anyone point out what I am missing?

The class module code, titled Class1:


Option Explicit
Dim rg As Range

Public Property Get StartCell() As Range
StartCell = rg
End Property
Public Property Let StartCell(Value As Range)
rg = Value
End Property


The test code:


Option Explicit

Sub ClassTest()

Dim Class1 As Class1
Dim rg As Range

Set Class1 = New Class1
Set rg = ThisWorkbook.Worksheets(1).Range("A1")
Class1.StartCell = rg

End Sub


When I run this I get the following error: "Run Time Error 91: Object Variable or With block variable not set".

Any ideas?

Bob Phillips
08-19-2008, 08:15 AM
Option Explicit

Private rg As Range

Public Property Get StartCell() As Range
Set StartCell = rg
End Property
Public Property Set StartCell(Value As Range)
Set rg = Value
End Property




Option Explicit

Sub ClassTest()

Dim Class1 As Class1
Dim rg As Range

Set Class1 = New Class1
Set rg = ThisWorkbook.Worksheets(1).Range("A1")
Set Class1.StartCell = rg

MsgBox Class1.StartCell.Address


End Sub

DRJD
08-19-2008, 08:35 AM
Thanks very much for your help.

I thought that I might be missing something like that.