ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Set Errors (https://www.excelbanter.com/excel-programming/392203-set-errors.html)

Ray Pixley[_2_]

Set Errors
 
I keep getting run time error 91, "object variable with block variable not
set" when doing the following:

First, create a Class Module called "CPM" whose content is

Option Explicit
Public Name as Collection

The code I'm trying to run is:

Option Explicit
Dim D As Collection
Dim C As CPM
Sub test()
Set C = New CPM
Set D = New Collection
D.Add "aa", "1"
subB
C.Name.Add "ss", "1" ' <----------------run time error
subA
End Sub
Sub subA()
C.Name.Add "tt", "2" ' <-------------- also want this to work
End Sub
Sub subB()
D.Add "bb", "3"
End Sub

I can add data to D object, but the C object constantly reports that run
time error. But the message makes no sense.

(My intent is to add a lot more public collections to CPM, and minimize the
amount of redundant coding that I have to do.)

Tim

Set Errors
 
You do not initialize the collection in the class - you only declare its
type.

Try this in the class_initialize code:

Set Name = new Collection

or just use

Public Name as New Collection


Tim


"Ray Pixley" wrote in message
...
I keep getting run time error 91, "object variable with block variable not
set" when doing the following:

First, create a Class Module called "CPM" whose content is

Option Explicit
Public Name as Collection

The code I'm trying to run is:

Option Explicit
Dim D As Collection
Dim C As CPM
Sub test()
Set C = New CPM
Set D = New Collection
D.Add "aa", "1"
subB
C.Name.Add "ss", "1" ' <----------------run time error
subA
End Sub
Sub subA()
C.Name.Add "tt", "2" ' <-------------- also want this to work
End Sub
Sub subB()
D.Add "bb", "3"
End Sub

I can add data to D object, but the C object constantly reports that run
time error. But the message makes no sense.

(My intent is to add a lot more public collections to CPM, and minimize
the
amount of redundant coding that I have to do.)




Ray Pixley[_2_]

Set Errors
 
It worked as long as

Public Name as New Collection

is placed in the class' declaration section.

Thank you.

"Tim" wrote:

You do not initialize the collection in the class - you only declare its
type.

Try this in the class_initialize code:

Set Name = new Collection

or just use

Public Name as New Collection


Tim


"Ray Pixley" wrote in message
...
I keep getting run time error 91, "object variable with block variable not
set" when doing the following:

First, create a Class Module called "CPM" whose content is

Option Explicit
Public Name as Collection

The code I'm trying to run is:

Option Explicit
Dim D As Collection
Dim C As CPM
Sub test()
Set C = New CPM
Set D = New Collection
D.Add "aa", "1"
subB
C.Name.Add "ss", "1" ' <----------------run time error
subA
End Sub
Sub subA()
C.Name.Add "tt", "2" ' <-------------- also want this to work
End Sub
Sub subB()
D.Add "bb", "3"
End Sub

I can add data to D object, but the C object constantly reports that run
time error. But the message makes no sense.

(My intent is to add a lot more public collections to CPM, and minimize
the
amount of redundant coding that I have to do.)






All times are GMT +1. The time now is 05:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com