Search code examples
excelvbauserform

What is the difference between Me-Object and UserFormName-Object?


I just found out that the Me-Object and UserFormName is not the same Object. Here my example:

I have two custom classes FilterLine and FilterModel. All you need to know is that FitlerModel has a property N which is set to = if newed up. There is also a UserForm called frmFilter.

Sub testFilter()
Dim Filterm As FilterModel

Set Filterm = New FilterModel

With New frmFilter
    Set .Model = Filterm
    .ExampleSub ' This is the interesting part
    .Show
End With
End Sub

Here the ExampleSub of the Userform frmFilter:

Public Sub ExampleSub()
Debug.Print Me.Model.N ' gives a 0

Debug.Print frmFilter.Model.N ' gives an error "Object not Found"
End Sub

I find this rather interesting what is going on here? Why are they different and how are they different?


Solution

  • They refer to different objects. A UserForm is just a Class that has a Default Instance, a free object created when you call the class by name.

    Me.Model.N

    References the object you instanced.

    frmFilter.Model.N

    Refers to the Default Instance of the Userform, which has no model associated with it.

    Good write-up on this issue here: https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/