Object variable or With block variable not set

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Standard module:

Rich (BB code):
Dim MyVar(1 To 10, 1 To 10) As MyClass

Set MyVar(1 To 10, 1 To 10) = New MyClass

MyVar(1, 1).Category = "Some category"
MyVar(2, 1).Fruit = "Some fruit"

MyClass:

Rich (BB code):
Private pCategory As Variant
Private pFruit As Variant
Public Property Get Category() As Variant Category = pCategory End Property Public Property Let Category(ByVal Cat As Variant) pCategory = Cat End Property
Public Property Get Fruit() As Variant Fruit = pFruit End Property Public Property Let Fruit(ByVal F As Variant) pFruit = F End Property

This line brings up a compile and syntax error message:

Rich (BB code):
Set MyVar(1 To 10, 1 To 10) = New MyClass

so I changed it to:

Rich (BB code):
Set MyVar(10, 10) = New MyClass
then this line returns a Object variable or With block variable not set:

Rich (BB code):
MyVar(1, 1).Category = "Some category"

Can someone tell me what is wrong?

Thanks
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You initialized one element of the array (MyVar(10, 10)) as a class instance, but then tried to assign a property to an element of the array that hasn't been initialized.

You need to initialize them all.
 
Upvote 0
You initialized one element of the array (MyVar(10, 10)) as a class instance, but then tried to assign a property to an element of the array that hasn't been initialized.

You need to initialize them all.

Can you show me how to initialize them all?
 
Upvote 0
See if this does what you need

Code:
Sub aTest()
    Dim MyVar(1 To 10) As MyClass
    Dim i As Long, v As Variant
    
    For i = 1 To 10
        Set MyVar(i) = New MyClass
    Next i
    
    MyVar(1).Category = "Category1"
    MyVar(1).Fruit = "Fruit1"
    MyVar(2).Category = "Category2"
    MyVar(2).Fruit = "Fruit2"
    'Just to check...
    For Each v In MyVar
        If v.Category <> "" Then
            MsgBox v.Category
            MsgBox v.Fruit
        End If
    Next v
End Sub

M.
 
Last edited:
Upvote 0
See if this does what you need

Code:
Sub aTest()
    Dim MyVar(1 To 10) As MyClass
    Dim i As Long, v As Variant
    
    For i = 1 To 10
        Set MyVar(i) = New MyClass
    Next i
    
    MyVar(1).Category = "Category1"
    MyVar(1).Fruit = "Fruit1"
    MyVar(2).Category = "Category2"
    MyVar(2).Fruit = "Fruit2"
    'Just to check...
    For Each v In MyVar
        If v.Category <> "" Then
            MsgBox v.Category
            MsgBox v.Fruit
        End If
    Next v
End Sub

M.

Thanks, will check it and get back to you shortly.

BTW, can you explain why this errors (as I initially mentioned)?

Rich (BB code):
Set MyVar(1 To 10, 1 To 10) = New MyClass



 
Upvote 0
You cannot set all array elements at once. Loop does the job by setting one at each step.

M.
 
Upvote 0
By the way, this worked for me

Code:
Sub aTest()
    [COLOR=#ff0000]Dim MyVar(1 To 10) As New MyClass
[/COLOR]    Dim i As Long, v As Variant
        
    MyVar(1).Category = "Category1"
    MyVar(1).Fruit = "Fruit1"
    MyVar(2).Category = "Category2"
    MyVar(2).Fruit = "Fruit2"
    MyVar(10).Category = "Category10"
    MyVar(10).Fruit = "Fruit10"
    
    'Just to check..
    For Each v In MyVar
        If Not v Is Nothing Then
            MsgBox v.Category
            MsgBox v.Fruit
        End If
    Next v
End Sub

M.
 
Upvote 0
You cannot set all array elements at once. Loop does the job by setting one at each step.

I see but why does this compile?

Rich (BB code):
Set MyVar(10, 10) = New MyClass


By the way, this worked for me

Code:
Sub aTest()
    [COLOR=#ff0000]Dim MyVar(1 To 10) As New MyClass
[/COLOR]    Dim i As Long, v As Variant
        
    MyVar(1).Category = "Category1"
    MyVar(1).Fruit = "Fruit1"
    MyVar(2).Category = "Category2"
    MyVar(2).Fruit = "Fruit2"
    MyVar(10).Category = "Category10"
    MyVar(10).Fruit = "Fruit10"
    
    'Just to check..
    For Each v In MyVar
        If Not v Is Nothing Then
            MsgBox v.Category
            MsgBox v.Fruit
        End If
    Next v
End Sub

M.

Thanks, I checked and it worked.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top