Consulting

Results 1 to 3 of 3

Thread: Solved: Setting a Range as a Class Property

  1. #1
    VBAX Regular
    Joined
    Jun 2008
    Posts
    16
    Location

    Solved: Setting a Range as a Class Property

    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:

    [vba]
    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
    [/vba]

    The test code:

    [vba]
    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
    [/vba]

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

    Any ideas?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jun 2008
    Posts
    16
    Location
    Thanks very much for your help.

    I thought that I might be missing something like that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •