VBA linking Class(es) and Userforms

saboh12617

Board Regular
Joined
May 31, 2024
Messages
82
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am a long time user of VBA but i never had the need to deal with userforms until recently i started digging into it.
I have been following OOP lessons at university and as a rubberduck user (in the sense that i read a lot of the VBA advices given on the blog) i found more coherent the idea to have the "running code" in procedures outside of the userform object, which end up being a "caller/receiver".

However i am struggling on the implementation as there is very few documentation on the latter.

I am aware that in a real project i would do a Class-based model, and then call its procedures via the userform events.

But i was trying to wrap the userform in another class, as one of its property, and then for example implement a display function which would allow me to display different instances of the same userform "frame".
I tried a simple example as given below but it wont work as i can not create two instances of the same userform at the same time. Is it normal?

VBA Code:
' Class1.cls
Option Explicit

Private Type tUF
  instanceUF As UserForm1
End Type

Private this As tUF

Private Sub Class_Initialize()
  Set this.instanceUF = UserForm1
End Sub

Public Sub display()
  this.instanceUF.Show
End Sub

VBA Code:
Sub testuf()
  Dim uf1 As Class1
  Dim uf2 As Class1
 
  Set uf1 = New Class1
  Set uf2 = New Class1
 
  uf1.display
  uf2.display   ' error, userform object does no longer exist
End Sub

The UserForm1 is an empty UserForm with no Sub.

If i assign the value after closing the first instance, then it works.
VBA Code:
Sub testuf()
  Dim uf1 As Class1
  Dim uf2 As Class1
 
  Set uf1 = New Class1
  
 
  uf1.display
  Set uf2 = New Class1
  uf2.display   ' no error
End Sub

However if i assign it before, then i suppose both my classes "instanceUF" are pointers to the same object.
Is there a turnaround possible? Should i implement a function "on close [X]" to handle it correctly? how to do so?

Thank you for your answers
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I forgot, here is the error raised
1719311877707.png
 
Upvote 0
However if i assign it before, then i suppose both my classes "instanceUF" are pointers to the same object.
Yes. That's what you are doing in the Class_Initialize event.

To have a diff UserFrom instance for each class instance, create a new userform as follows:
VBA Code:
Private Sub Class_Initialize()
  Set this.instanceUF = New UserForm1
End Sub
 
Upvote 0
Solution
Oh...
Indeed it makes sense, what doesn't make sense is that i did not need to put "New" in the first place. But i guess it has to do with the "Auto instantiation" of the Userforms. They are a bit confusing at the beginning due to those, i guess, "user friendly" features that automatically do things and by doing so do not raise errors or alerts...

Thanks for your time, it works.
 
Upvote 0
But i guess it has to do with the "Auto instantiation" of the Userforms.
Yes. This can be confusing. As you said, this is due to the UserForms Instancing VB_PredeclaredId attribute value being set to True by default. However, subsequent instances can't be created without the New keyword unless the previous default instance is destroyed.
 
Upvote 1

Forum statistics

Threads
1,223,912
Messages
6,175,344
Members
452,638
Latest member
Oluwabukunmi

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