Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Class Module Confusion

    Hello everybody,

    I am experimenting with class modules, and getting stuck. I want one of the read/write properties to be a range. I tried doing so as follows, in the class module:
    <pre>-----
    Private xDataRange As Range
    -----
    Public Property Get DataRange() As Range

    DataRange = xDataRange

    End Property
    -----
    Public Property Let CDRange(ByVal UserRange As Range)

    xDataRange = UserRange

    End Property
    -----
    Private Sub Class_Initialize()

    xDataRange = Range("A1:B2")

    End Sub
    -----
    </pre>

    In a normal module, the following code gives a "Object variable or With block variable not set" error (error 91):
    <pre>-----
    Dim TestInstance As New TestObject
    MsgBox TestInstance.DataRange.Address
    -----
    </pre>

    Any advice? Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Class Module Confusion

    Well, I normally work in Access, but no matter which application you work in, setting an object variable requires the Set keyword. You've declared xDataRange as a Range object, but you aren't using the Set keyword to instantiate that object. Try adding it and see if that fixes the problem. For example:

    <pre>Private Sub Class_Initialize()
    Set xDataRange = Range("A1:B2")
    End Sub</pre>


    Actually, I would expect it to require at least a sheet reference as well, depending on where you created the class module, but as I said, Access is my weapon of choice, so I'm not up on the ins and outs of Excel class modules.
    Charlotte

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Class Module Confusion

    Hi,

    Yes, that was the problem. Silly me, so busy learning new things I am forgetting about old things.

    Thanks!

Posting Permissions

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