PDA

View Full Version : Class related Stuff Userforms



DocAElstein
12-26-2018, 04:27 PM
Some old notes, recovered / repaired from here
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html
7 May 2016 up to about the 11 May, then possibly the original Problem and Solution Summary , beefed up a bit after the thread Help Understanding Class Instancing. Can't Set ws = New Worksheet. Intellisense offers (https://www.excelforum.com/excel-programming-vba-macros/1138804-help-understanding-class-instancing-cant-set-ws-new-worksheet-intellisense-offers-it.html) that went from 11 May to 16 May. The last edit on the original of all this was on the 19 May, (but I may have messed the last posts up a bit)






This is what I got. Rory did it for me
58585858https://i.postimg.cc/3dLjnXXZ/User-Form-Rory-did-for-me.jpg (https://postimg.cc/3dLjnXXZ)
https://i.postimg.cc/YC7X28jT/User-Form-Rory-did-for-me.jpg (https://postimg.cc/3dLjnXXZ)

5859https://i.postimg.cc/nCVY4RDz/User-Form-Rory-did-for-me.jpg (https://postimg.cc/nCVY4RDz)

'Static fm As ufResults ' instead of this I have it as a Global variable so that i can check it elsewhere, and I add it and check a box on opening the file
'Dim fm As ufResults ' This would result in the UserForm "Dieing" a t every code ending
If Fm Is Nothing Then Set Fm = New ufResults ' if this line was not yet done, then a new instance of the class ufResults with the name fm is made
If Not Fm.Visible Then Fm.Show False 'make sure UserForm is always there,
'Start of Part only done for entry in the three C column Ranges and no check for Calculate all else. -- Things here are that can be done quickly, 1 "row" stuff!! http://www.excelforum.com/showthread.php?t=1107695&p=4380613&highlight=#post4380613

DocAElstein
12-26-2018, 04:28 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4380945
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24168&viewfull=1#post24168




VBA to Check If User Form Buttons are checked and Check a Button on Worksheets Open

If UserForm1.Value = TrueNot working ! :(

_1) VBA to Check If User Form Buttons are checked
and
_2) Check a Button with a code on Worksheets Open ( or with any Code )
_......

(_.....EDIT : Thursday 11th May 2016: Summary of problem and solution at this Post:
http://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open-2.html#post4384440
_.....)


Hi
I am getting OK now with normal VBA thanks to Forum participation. :) . But I have not much experience with User Forms. Another Member kindly added a User Form for me in my File. Sadly he is no Longer with us.... ; )
It is the only User Form in my File.

I added myself two Option Buttons and a Check Button. .... :)

_1 ) Question 1)
I wish to check at a code line If those Buttons are checked. ( That check needs to be done in a code in a Normal Module, or a Worksheets Code Module )

According to my Googling it should be dead easy.

So I did a quick code to check if I could check if the to be checked things are checked.


Sub CheckOptionCheckCheckedCheck()
Dim v As Variant
Let v = StatusBarNormal.Value
Let v = OptionButton2.Value
Let v = Refresh.Value
End Sub

I noticed that should I use lower case in those code lines for either Button Name, such
statusbarnormal
Then the VB corrects it to
StatusBarNormal

So it does appear to recognise those “things” ( Objects? )

Also code lines of that form do work within the Codes
Private Sub StatusBarNormal_Click()
Private Sub OptionButton2_Click()
Private Sub Refresh_Click()
( These codes are located in the User Form somehow.. )

However, running my Demo Code from a Normal Code Module or Worksheet Module it errors. It highlights any of those three , such as StatusBarNormal.. and says that the variable has not been Defined

Can anyone see if I am doing anything obviously wrong due to my ignorance in this area ?
_.....................................

BTW. I do have a workaround. ( I share it here.. )
My workaround ( for the Refresh Check box ), ( which works ), is as follows.

In a Normal Module ( Module Globies ) I Have this


Option Explicit
Public RefreshCColumn As Boolean 'For Check box to recalculate all values

Then in my code, ( in Sheet1 Code Module )
Private Sub Worksheet_Change(ByVal Target As Range)
I have this where I want to check if the Option Button “Refresh” is checked

'Part 6) Possible Refresh ( Afforderlisch wenn a value is changed on an existing column to Refresh all )' Will be done for check in Refresh Check box
If RefreshCColumn = True Then

The corresponding User Form Code looks like this:

Private Sub Refresh_Click()
If Refresh.Value = True Then
Let RefreshCColumn = True 'Global variable as Flag for if Refresh is checked
Else
End If
End Sub
_.........................

The workaround for my two Option Buttons ( which also works ) is that they turn the Status Bar ( that thing down there at the left ) on and off thus

Private Sub StatusBarNormal_Click()
Application.DisplayStatusBar = False 'Normal Situation
End Sub

Private Sub OptionButton2_Click()
Application.DisplayStatusBar = True
End Sub

Then in the code
Private Sub Worksheet_Change(ByVal Target As Range)
I have this line to check that state..

'All From here Parts 2) to 5) will be done for an appropriate check to do all other as well as quick stuff
'Start Other parts ---( Parts 2) - 5) )----------------------------------------------------
If Application.DisplayStatusBar = False Then 'Parts to complete all calculations and Totals Outputs
'I have a radio ( option ) buttons on the UserForm and can make Status Bar False, ( That is to say in it's normal state )

So I have a solution., But I am still googling that I can directly check if my Option Buttons and Check boxes in my UserForm In a code like my first given above. ( Or does those codes only work in the User Form. I change those 3 Privates to Public and that had no effect )

What am I doing wrong?

_......................
_2 )Question 2)
The second question is if I can check one of those Buttons with a code line? ( Ideally in my This Private Sub Workbook_Open() code so that i can set the User Forms Check boxes as i wasn’t on opening )
This second question is not too important as I guess it may be a bit difficult, as the Code lines which somehow “make” the User Form are in my
Private Sub Worksheet_Change(ByVal Target As Range)

'Part 0.5 )RoryA UserForm http://www.excelforum.com/excel-programming-vba-macros/1086822-contents-of-an-array-derived-from-a-macro-into-a-fixed-display-window-user-form.html#post4152417
Static fm As ufResults: If fm Is Nothing Then Set fm = New ufResults: If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,

_......
I could not find by googling at all, how to check a Button from a code.
_................................................. ...........

Thanks for any help
Alan

_.................................
P.s.
If it helps,
Here is my File. ( “ProAktuellex8600x2.xlsm" ) ( It is a Daily Nutrition Consumption Protocol ! )
I Apologise that I have not reduced it to the minimum, but this is very bit difficult as many things like Ranges are hard coded. So I will get in a real mess if I try chopping rows out etc..
On opening you will be asked if you want to Initialise. It is OK to hit OK to that! ( Puts the Daily Coffee Intake the Daily Nutrition Consumption Protocol ! )
https://app.box.com/s/fpztob9pcp92fl6hh81zgpzumw9ntcp0
I thank you kindly, once again,..... and thanks for reading ! .... ;)

DocAElstein
12-26-2018, 04:28 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4380955
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4380967
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4380992
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24169&viewfull=1#post24169







Answer from Richard ( RIP (: ) (https://www.excelfox.com/forum/showthread.php/2826-RIP-Richard-Buttrey)


Hi,

There's a lot to comprehend there. You'd be better advised to upload the workbook so that we can see this in context.
Clearly explain where you want to check for the condition of your option buttons.

However try something like


If UserForm1.OptionButton1 Then
'....your code if option button has been selected
Else
' Code (if any) if ob not selected
End If



Hi Richard


Thanks for the quick reply.
The File I gave a link to at the end of Post #1
( Sorry it is a bit too big to upload, and reducing the data is difficult as I explained. )

Also I showed where I Have the two required If 's
Sorry there was a lot there. :(
I was also sharing my Workaround, and trying to be as concise as possible. :(

Thanks very much for your code I will try it out :)
Alan :)

EDIT:
P.s.
Richard: I ammended the title after this was solved, as your answer was excactly what many people thought and indeed what I still Google. I know why. This will catch a lot of people out so I will do a last follow up Post to Explain
Alan


Hi Richard,

Ok I tried the demo code kike this following your suggestion,


Sub CheckOptionCheckCheckedCheck()
Dim v As Variant
Let v = ufResults.StatusBarNormal.Value
Let v = ufResults.OptionButton2.Value
Let v = ufResults.Refresh.Value
End Sub

That does not error. :). - I guess I should have twigged to that. I never stop telling OP’s to fully reference things like Workbooks and Worksheets thus:
WB.
And
Ws.
Etc....

Logical that the same applies to User Form things
UserForm.
And that explains why of course it worked within the User Form ( ufResults ) codes!

_..................

But in the demo code, ( which I have in a normal module ) it gives False for
v
even if I check the Button StatusBarNormal
:(

I also put this line,
Dim v: v = ufResults.StatusBarNormal.Value
in the
Private Sub Worksheet_Change(ByVal Target As Range)
In Sheet 1 ,
then
put a stop on it,
then
checked Button StatusBarNormal
then
initiated running of
Private Sub Worksheet_Change(ByVal Target As Range)
( by putting a number in any column C column where there is a Food in column A )

Then I Debug Mode F8 to go past
v
Then I hover to see what is in v , and it is still False, despite Button
StatusBarNormal
Being checked.

So I am not quite there yet....

I will keep at it

Thanks again for the reply

Alan

DocAElstein
12-26-2018, 04:28 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4381146
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4381201
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24170&viewfull=1#post24170





Rory starts chipping in 05-08-2016 8 May 2016 ….._
_..... and away we go


HOw/when are you loading the form? If it's not still loaded when you run that code, you will get False.


Hi Rory,
Morning. Thanks fort he reply.

I have been having a play after what you said...googling etc on Loads etc.
Sorry I am still totally ignorant with user Forms. I still have not been able to understand your question.
_.............

These bits are towards the start of the Worksheet_Change code:

'Part 0.5 )RoryA UserForm http://www.excelforum.com/excel-programming-vba-macros/1086822-contents-of-an-array-derived-from-a-macro-into-a-fixed-display-window-user-form.html#post4152417
Static fm As ufResults: If fm Is Nothing Then Set fm = New ufResults: If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
They seem to make the user Form Pop Up…. From then on it always there to “see.”
If I check maybe the first option button “StatusBarNormal”. Then initiate a run of the Worksheet_Change code a couple of times ( any entry alongside a Food in column C ),
I see this and all is well :)
459606
_..................................

I then check the second two boxes as shown below
459605

After that my two workarounds work, ( detailed in post #1 - For example you see in the second Image my Global variable set in the Refresh Check Button code has changed to True ). So somehow those Button check boxes must have been “read”. But I cannot seem to put any code line anywhere ( in this code or elsewhere ) that returns me anything other than False for
ufResults.Refresh.Value
or
ufResults.OptionButton2.Value
( checking the value of my Global variable workaround does as I expect. ( I have to recheck that Refresh Button to get it True, but that is how I wrote the code ) )

Sorry it is very difficult to follow this Thread. One case may be where a file is useful! My File is still linked in Post#1 if you had time to check it.
I just added a couple of Debug Lines. They are responsible for the results in the immediate window on those two screen shots

StatusDeBuger: Dim v: v = ufResults.OptionButton2.Value: Debug.Print "ufResults.OptionButton2.Value "; v
And

EresDeBuger: v = ufResults.Refresh.Value: Debug.Print "ufResults.Refresh.Value "; v
GlobieDeBuger: v = RefreshCColumn: Debug.Print "Global RefreshCColumn= "; v
Those are at the two points where I want to check to see if the Buttons are checked. That is where I do not understand that I cannot get a True result ever.
( I just reloaded my File with those new Debug lines in, same link as in Post #1 _..
https://app.box.com/s/fpztob9pcp92fl6hh81zgpzumw9ntcp0
_.. )

This is an awkward one to follow, sorry.
Alan

P.s. I have discovered a ( new ) Phenomena, just now! ....
This

Static fm As ufResults: If fm Is Nothing Then Set fm = New ufResults: If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
Does not work like this:

Static fm As ufResults: If fm Is Nothing Then Set fm = New ufResults
If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
In the first case the second condition is not checked if the first is not met – it ignores the : thing which should tell it there is the next code line!!
Interesting ( But I modified my code to the second case, and it has no effect with my current problem )






_
_Edit.. On Editiing I can no longer see those atttatchments, how poo!
bloody EF Software!!





https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwhVTFaD469mW9wO194AaABAg.9gJzxwFcnPU9gORqKw5t W_ (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwhVTFaD469mW9wO194AaABAg.9gJzxwFcnPU9gORqKw5t W_)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugyb8nmKKoXvcdM58gV4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugyb8nmKKoXvcdM58gV4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwvvXcl1oa79xS7BAV4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwvvXcl1oa79xS7BAV4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgxvIFArksPprylHXYZ4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgxvIFArksPprylHXYZ4AaABAg)
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg (https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)




https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://eileenslounge.com/viewtopic.php?f=27&t=35521&p=276185#p276185 (https://eileenslounge.com/viewtopic.php?f=27&t=35521&p=276185#p276185)
https://eileenslounge.com/viewtopic.php?p=276185#p276185 (https://eileenslounge.com/viewtopic.php?p=276185#p276185)
https://eileenslounge.com/viewtopic.php?p=276185#p276185 (https://eileenslounge.com/viewtopic.php?p=276185#p276185)
https://eileenslounge.com/viewtopic.php?p=276673#p276673 (https://eileenslounge.com/viewtopic.php?p=276673#p276673)
https://eileenslounge.com/viewtopic.php?p=276751#p276751 (https://eileenslounge.com/viewtopic.php?p=276751#p276751)
https://eileenslounge.com/viewtopic.php?p=276754#p276754 (https://eileenslounge.com/viewtopic.php?p=276754#p276754)
https://eileenslounge.com/viewtopic.php?f=30&t=35100&p=274367#p274367 (https://eileenslounge.com/viewtopic.php?f=30&t=35100&p=274367#p274367)
https://eileenslounge.com/viewtopic.php?p=274368#p274368 (https://eileenslounge.com/viewtopic.php?p=274368#p274368)
https://eileenslounge.com/viewtopic.php?p=274370#p274370 (https://eileenslounge.com/viewtopic.php?p=274370#p274370)
https://eileenslounge.com/viewtopic.php?p=274578#p274578 (https://eileenslounge.com/viewtopic.php?p=274578#p274578)
https://eileenslounge.com/viewtopic.php?p=274577#p274577 (https://eileenslounge.com/viewtopic.php?p=274577#p274577)
https://eileenslounge.com/viewtopic.php?p=274474#p274474 (https://eileenslounge.com/viewtopic.php?p=274474#p274474)
https://eileenslounge.com/viewtopic.php?p=274579#p274579 (https://eileenslounge.com/viewtopic.php?p=274579#p274579)
https://www.excelfox.com/forum/showthread.php/261-Scrolling-Marquee-text-on-Userform?p=864&viewfull=1#post864 (https://www.excelfox.com/forum/showthread.php/261-Scrolling-Marquee-text-on-Userform?p=864&viewfull=1#post864)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

DocAElstein
12-26-2018, 04:29 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382657
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24171&viewfull=1#post24171



Still struggling here. :mad: :( :confused:

Can anyone help

_......




Possible a User Form expert may see that I am missing something fundamental ? .

I am still not able to get anything other than False when using debug lines ( which I have at the points where I want to use such a check )


StatusDeBuger: Dim v: v = ufResults.OptionButton2.Value: Debug.Print "ufResults.OptionButton2.Value "; v

EresDeBuger: v = ufResults.Refresh.Value: Debug.Print "ufResults.Refresh.Value "; v

The full story is given, and the File is linked, in the previous posts

Here again I give the relavent parts of the Worksheet_change code
( Apologies again that I can not simplify this code and test data. As mentioned before this is particularly difficult in this case to do )

Towards the start I have this, which appears to make the User Form come up


Static fm As ufResults
If fm Is Nothing Then Set fm = New ufResults
If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,

Then a bit further down I have this DeBug Line


StatusDeBuger: Dim v: v = ufResults.OptionButton2.Value: Debug.Print "ufResults.OptionButton2.Value "; v

Towards the end of the code I have a line which puts the contents of an Array into a list in the User Form

fm.lstResults.ColumnCount = UBound(ArrDisplaySP(), 2): fm.lstResults.List = ArrDisplaySP(): fm.Repaint

Shortly after that I have my second Debuger line


EresDeBuger: v = ufResults.Refresh.Value: Debug.Print "ufResults.Refresh.Value "; v

Thank you
Alan

P.s. The File Link again, details about it, how to use it etc.. in first post

https://app.box.com/s/fpztob9pcp92fl6hh81zgpzumw9ntcp0

DocAElstein
12-26-2018, 04:32 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382670
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24172&viewfull=1#post24172





You need to be referring to fm.OptionButton2 not ufResults.OptionButton2

If you need access to these from other routines, you should move the declaration of fm appropriately.

DocAElstein
12-26-2018, 04:32 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382748
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24173&viewfull=1#post24173






Hallo !
Thanks for the replies :)

Quote Originally Posted by rorya View Post
You need to be referring to fm.OptionButton2 not ufResults.OptionButton.....

So I tried
v = fm.OptionButton2.Value
instead in the Worksheet_change Code

It works !!! – gives True or False appropriately !
Thanks!! :)


Here goes the Nut rambling again, sorry about that..
Clearly I have not a clue about user Forms and sadly running out of time to learn.
All I see is in the VB Project Window is ufResults, so I thought that “is” the UserForm ??
That tied up with what Richard suggested. He suggested
UserForm1.
Now... if I insert a new Userform, it gets the name
UserForm1.
The one you did for me has this name
ufResults.
So clearly There seems some logic to what I tried to do?
And the lines with that in do not error, ( just always give False )

_..........................
Quote Originally Posted by rorya View Post
If you need access to these from other routines, you should move the declaration of fm appropriately.

Sorry i do not ( did not ) get it as usual, I guess it means to do with to get the last Three lines to work in this code in a normal module, may be ?

Sub CheckOptionCheckCheckedCheck()
Dim v As Variant
Let v = ufResults.StatusBarNormal.Value 'Always gives False
Let v = ufResults.OptionButton2.Value 'Always gives False
Let v = ufResults.Refresh.Value 'Always gives False
'Let v = fm.StatusBarNormal.Value 'At attempt to run code error with complie "error variable not defined"
'Let v = fm.OptionButton2.Value
'Let v = fm.Refresh.Value
End Sub


I spent some considerable time being very precise about referring to Ranges correctly but clearly I have no idea what / where the parallel is with User Forms...

So, can I ask
_ What is fm ?
And
_ What is ufResults?
_ How come I can access ( I mean it does not error in those lines above ) ufResults from elsewhere,
but not fm from other than where it is...

Or is this parallel to Code Modules, like this: If I Declared it in a normal module, then would I be able to access it elsewhere. – ( Sorry with this one I hesitate to experiment. – I do not want to create UserForms all over the place hap hazadly !!)*****

In my VB Project Window ufResults does not seem to be tied down to a Sheet. And , again, I cannot find this “thing” fm anywhere! What is fm!!!

Or how about.
ufResults is a sort of class thing, and fm is one instance of it. *****


Alan
_............................

*****Edit: OKI I did anyway experiment
Just to balance out a bit...
I did this .... I “kloned” :)
_- got two with things showing

If fm Is Nothing Then Set fm = New ufResults
If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
Static fm2 As ufResults
If fm2 Is Nothing Then Set fm2 = New ufResults
If Not fm2.Visible Then fm2.Show False

May be I do get it... a bit,
I did this, and the second code does not work...

Sub MakeAInstanceOfufResults()
Static fm2 As ufResults
If fm2 Is Nothing Then Set fm2 = New ufResults
If Not fm2.Visible Then fm2.Show False
Dim v As Variant
Let v = fm2.StatusBarNormal.Value 'At attempt to run code error with complie "error variable not defined"
Let v = fm2.OptionButton2.Value
Let v = fm2.Refresh.Value
End Sub
Sub CheckOptionCheckCheckedCheckOffm2()
Dim v As Variant
'Let v = fm2.StatusBarNormal.Value 'At attempt to run code error with complie "error variable not defined"
'Let v = fm2.OptionButton2.Value
'Let v = fm2.Refresh.Value
End Sub
_............................

But now I do this in a normal Module:

Public fm2 As ufResults
Public fm As ufResults
(_....And this in my Worksheet:Change code..

'Static fm As ufResults
_.......)
and all is well :)
All the following work , that is to say give the results I expect.


Option Explicit
'Public RefreshCColumn As Boolean 'For Check box to recalculate all values ' I do not need this now as i think the Nut has User Forms sussed...
Public fm2 As ufResults
Public fm As ufResults
Sub MakeAInstanceOfufResults()
If fm2 Is Nothing Then Set fm2 = New ufResults
If Not fm2.Visible Then fm2.Show False
Dim v As Variant
Let v = fm2.StatusBarNormal.Value
Let v = fm2.OptionButton2.Value
Let v = fm2.Refresh.Value
End Sub
Sub CheckOptionCheckCheckedCheckOffm2()
Dim v As Variant
Let v = fm2.StatusBarNormal.Value
Let v = fm2.OptionButton2.Value
Let v = fm2.Refresh.Value
End Sub
Sub CheckOptionCheckCheckedCheckOffm()
Dim v As Variant
Let v = fm.StatusBarNormal.Value
Let v = fm.OptionButton2.Value
Let v = fm.Refresh.Value
End Sub

There, you see, preparing concisely in a Thread Reply can help you sometimes get there yourself...

I Thanks us, you and me
Alan

DocAElstein
12-26-2018, 04:33 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382778
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24174&viewfull=1#post24174





Quote Originally Posted by Doc.AElstein View Post
Or how about.
ufResults is a sort of class thing, and fm is one instance of it. *****
Bingo! Hence your subsequent results.

Unlike normal classes, userforms are auto-instantiating, so you can simply call them by name and a new instance of the class is created. That's why the changes weren't being reflected in your code - you were referring to two separate instances of the form.


There, you see, preparing concisely in a Thread Reply can help you sometimes get there yourself...
Exactly. :)

DocAElstein
12-26-2018, 04:33 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382802
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382894
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24175&viewfull=1#post24175




Thanks Rory, :)

Just one last thing to make sure I got it..
Originally Posted by rorya
... That's why the changes weren't being reflected in your code - you were referring to two separate instances of the form......


I was referring originally ( in my Debug check lines ) to
ufResults..

That was not an instance was it, as it is the original ( Blueprint ).... Or is it... In this case....

Alan

_.___________
Rory (Romperstomper) 05-10-2016, 10 May 2016

Yes it is. As soon as you use ufResults for the first time, a new instance of the form is created. It's confusing because you effectively get a variable of the same name as the class. So that instance was not the same as the fm instance. (If you'd added a Debug.Print Userforms.Count line in there, you'd have seen 2 forms)

For a simple demo, in a new workbook, add a blank userform, then add this code and run it:

Sub foobar()
Dim fm As UserForm1
Set fm = New UserForm1 ' created an instance of the form
MsgBox UserForms.Count ' you'll see 1 here
UserForm1.Caption = "Loaded another instance" ' this loaded a new instance of the form and assigned a variable called Userform1
MsgBox UserForms.Count ' so you'll see 2 here
End Sub

DocAElstein
12-26-2018, 04:33 PM
SCNASSAN

DocAElstein
12-26-2018, 04:35 PM
For later use

https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg (https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5 (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl )
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh)
https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2 (https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2)
https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugz-pow-E8FDG8gFZ4l4AaABAg.9f8Bng22e5d9f8hoJGZY-5 (https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugz-pow-E8FDG8gFZ4l4AaABAg.9f8Bng22e5d9f8hoJGZY-5)
https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxev2gQt7BKZ0WYMfh4AaABAg.9f6hAjkC0ct9f8jleOui-u (https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxev2gQt7BKZ0WYMfh4AaABAg.9f6hAjkC0ct9f8jleOui-u)
https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxg9iT7MPWGBWruIzR4AaABAg (https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxg9iT7MPWGBWruIzR4AaABAg)
https://www.youtube.com/watch?v=tzbKqTRuRzU&lc=UgyYW2WZ2DvSrzUKnJ14AaABAg (https://www.youtube.com/watch?v=tzbKqTRuRzU&lc=UgyYW2WZ2DvSrzUKnJ14AaABAg)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)



https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eekDyfS0 CD (https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eekDyfS0 CD)
https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eevG7txd 2c (https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eevG7txd 2c)
https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg (https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg)
https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzytUUVRyw9U55-6M54AaABAg (https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzytUUVRyw9U55-6M54AaABAg)
https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzCoa6tOVIBxRDDDbN4AaABAg (https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzCoa6tOVIBxRDDDbN4AaABAg)
https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgyriWOelbVnw4FHWT54AaABAg.9dPo-OdLmZ09dc21kigjmr (https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgyriWOelbVnw4FHWT54AaABAg.9dPo-OdLmZ09dc21kigjmr)
https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzDQfo5rJqyVwvv2r54AaABAg (https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzDQfo5rJqyVwvv2r54AaABAg)
https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzHTSka7YppBdmUooV4AaABAg.9cXui6zzkz09cZttH_-2Gf (https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzHTSka7YppBdmUooV4AaABAg.9cXui6zzkz09cZttH_-2Gf)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9iMmBDtf4m1 (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9iMmBDtf4m1)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxFIZ858qf7w_uA9bd4AaABAg.9dKpEpUk3YT9dVEGnka6 yj (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxFIZ858qf7w_uA9bd4AaABAg.9dKpEpUk3YT9dVEGnka6 yj)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9brzh_99JF9 (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9brzh_99JF9)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9bsrQIgXb3L (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9bsrQIgXb3L)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmx0REIz41 (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmx0REIz41)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9bmyko2YUvQ (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9bmyko2YUvQ)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmzpPqfLRD (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmzpPqfLRD)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZwbV_Y_7UFzHwNBh4AaABAg.9dKb0Vc7MOB9dVK8si3o nt (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZwbV_Y_7UFzHwNBh4AaABAg.9dKb0Vc7MOB9dVK8si3o nt)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugx6Ec_r4kb9EYOVgIt4AaABAg.9dOW613fb8V9dVIJECZI dC (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugx6Ec_r4kb9EYOVgIt4AaABAg.9dOW613fb8V9dVIJECZI dC)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9cLjhPi az (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9cLjhPi az)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZy1NAMBx5Uv4U2cJ4AaABAg.9f0XX-_JaGp9g9bYLMZiIy (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZy1NAMBx5Uv4U2cJ4AaABAg.9f0XX-_JaGp9g9bYLMZiIy)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyL-xp8IiiahmQ12kJ4AaABAg.9f7xHCpAEx29g9asFhVFfT (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyL-xp8IiiahmQ12kJ4AaABAg.9f7xHCpAEx29g9asFhVFfT)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxRxyFNNp3WHTzuiJJ4AaABAg.9fFR6ECmXk69g9afNBcS 4Z (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxRxyFNNp3WHTzuiJJ4AaABAg.9fFR6ECmXk69g9afNBcS 4Z)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwsdMh0FGDfvA249_B4AaABAg.9fLR6FHCIVI9g9aLlUyz og (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwsdMh0FGDfvA249_B4AaABAg.9fLR6FHCIVI9g9aLlUyz og)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9_4422N zK (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9_4422N zK)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugwyy8JXr56HJ8m_od94AaABAg.9gSFgqqJQNV9gTXco41b 5l (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugwyy8JXr56HJ8m_od94AaABAg.9gSFgqqJQNV9gTXco41b 5l)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTYl6Rld pA (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTYl6Rld pA)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfhAWU9 ju (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfhAWU9 ju)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfuYQGm Ua (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfuYQGm Ua)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTg3AmMP Uc (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTg3AmMP Uc)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTgEqh5w do (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTgEqh5w do)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxmUK0S_aZVZWz8-gt4AaABAg.9gLc3DfWfHl9gTZ3y6fL1H (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxmUK0S_aZVZWz8-gt4AaABAg.9gLc3DfWfHl9gTZ3y6fL1H)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZloYeY2wQr7-xTOh4AaABAg.9gB2bbbs9mB9gTZUkNYI8e (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZloYeY2wQr7-xTOh4AaABAg.9gB2bbbs9mB9gTZUkNYI8e)
https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzlM96nGEhW9J1Gpgd4AaABAg.9fmOFVcXZh49gT_8CYeQ gz (https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzlM96nGEhW9J1Gpgd4AaABAg.9fmOFVcXZh49gT_8CYeQ gz)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

DocAElstein
12-26-2018, 04:35 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4382894
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4383066
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24176&viewfull=1#post24176
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24178&viewfull=1#post24178




……………… As soon as you use ufResults for the first time, a new instance of the form is created. It's confusing because you effectively get a variable of the same name as the class. So that instance was not the same as the fm instance. (If you'd added a Debug.Print Userforms.Count line in there, you'd have seen 2 forms)

For a simple demo, in a new workbook, add a blank userform, then add this code and run it:

Sub foobar()
Dim fm As UserForm1
Set fm = New UserForm1 ' created an instance of the form
MsgBox UserForms.Count ' you'll see 1 here
UserForm1.Caption = "Loaded another instance" ' this loaded a new instance of the form and assigned a variable called Userform1
MsgBox UserForms.Count ' so you'll see 2 here
End Sub

Thanks Rory.
That is very helpful. These niggly small discrepancies can be very annoying.
Nice to get them cleared up
This also clears up some problems i had once when looking at different Add-Ins and getting confused which one was running. I had been changing the class UserForm, rather than the Instance of it. Or the other way around, or whatever... At the end of the day I thought I was changing a code somewhere I was not. ( I sorted all that out by exporting the UserForm , changing the name of the in the File then importing the original back.....and then being very careful to check where I was when I changed anything )
_
So Fuck he’s off agiain...

In these two lines The first line changes a Property of the instance, which then “dies” when the File closes. No Changes are made to the User Form ( Class ).
So when I “make this “ instance we have a “ByValue Call” here, as it were, passing a Copy of the User Form to the screen.
Not 100% sure, yet what a Load is... I have a Load of...____x ... when...?

The second code line seems to have no effect. ( I guess I have to change things to the class manually )
fm2.Caption = "Poo"
ufResults.Caption = "Poo"
I said The second seems to have no effect
_........
So I thought it did nothing.. now I know better..
So
_ ....I did what you said, added a User form to a new File, ( and I added a button.. because I could :) )
I get a Variable of that name which refers to a new instance, but then I cannot seem to add a caption to it... or I can , you did.. and with my modified version of your code below I see “it” . So maybe it is indeed suddenly a new instance with the same name. And as it is an Instance, when I save and close and reopen the File, I find that the name on UserForm1 is “UserForm1”, and not as I aptly named the instance in the code below:
"Loaded another instance annoyingly with the same name of The User Form Class" as in the VB Project .

Option Explicit
Sub foobar_Loadof____() ' http://www.excelforum.com/showthread.php?t=1138300&p=4382894#post4382894
Dim fm3 As UserForm1: ' ' ' Preparing a "Pointer" to an Initial "Blue Print" in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Objec of this type ) . This also us to get easily at the Methods and Properties throught the applying of a period ( .Dot) ( intellisense )
Set fm3 = New UserForm1 ' Created an instance of the form Still just an "un Filled in" Blueprint. A copy of the Blue print
MsgBox UserForms.Count ' you'll see 1 here
If Not fm3.Visible Then fm3.Show False 'I see my new instance, ByVal Copy of the UserForm1, A Load of_____
fm3.Caption = " I Loaded this fm3 instance "
MsgBox UserForms.Count ' see I have 1
Dim v: v = UserForm1.CheckBox1.Value 'Always will return False, ( unless I check it on this instance ), - A Load of it has just been done ;)
MsgBox UserForms.Count ' I see I have 2
If Not UserForm1.Visible Then UserForm1.Show False 'I see something, and... either this line or the next or the previous were capable of making it. The last one did
MsgBox UserForms.Count ' I see I have 2
UserForm1.Caption = "Loaded another instance annoyingly with the same name of The User Form Class" ' this loaded a new instance of the form and assigned a variable called Userform1
MsgBox UserForms.Count ' so you'll see 2 here - either of the last three lines will cause this
'
Dim fm4 As UserForm1
' If Not fm4.Visible Then UserForm1.Show False' This will not work The fact that I can create an instance of the Class ( With the name of the class ) without the Dim Set pair is an interesting oddity.
End Sub

_.........................

I think I really have this now as far as I am ever likely to need. Thanks very much. But I think in this case you will agree with me that we are indeed talking a “Load of ..... “ . This goes right back to your original answer which ,
Originally Posted by rorya
HOw/when are you loading the form? If it's not still loaded when you run that code, you will get False.

There you were talking a Load of..... i was also talking about a Load of...... Unknowingly I had created another instance in testing for ufResults button values, A Load of ... ByVal .. Copy of... and I had been doing checks on that instance rather than that instance which was loaded.
( So I expect I cannot change the Class Properties by code, or not at any rate through UserForm1.___ as that refers to the instance. ( and even if I could do such , I do not see the point. Doing it manually is easy enough.. ) )

The fact that I can create an instance of the Class ( With the name of the class ) without the
Dim
Set
pair is an interesting oddity. But who knows... might have its uses.
_................................................. ............

I think I answered my other question as well, - How to uncheck a Button with a code. If a button is checked, I can uncheck it with
Let fm2.Refresh.Value = False ‘ Refresh is the Button name

That does have the annoying habit of kicking off the code behind it. But if I do not want that then this in the first line sorts that out
If Refresh.Value = False Then Exit Sub

Thanks again
Alan

DocAElstein
12-26-2018, 04:35 PM
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24179&viewfull=1#post24179
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4383265
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4383231



You can change the class properties through code, using the Designer object.
romperstomper: - You can change the class properties through code, using the Designer object.


[QUOTE=Doc.AElstein;4383265]OK, thanks, can't think why I would want to do that, but worth knowing.
Actually I was just going to Edit BTW. my last post and say I have it well solved now thanks :) .....
_ the last bit I wanted I just did and was easy now. -As I had changed my Declaration of fm to be a Globie, as part of the Experiments today.( and like you originally said :)
Originally Posted by rorya
..... need access to these from other routines, ..move ..declaration of fm appropriate...
....... I was able to just add a few lines to create the fm instance and check the button I wanted to on opening the file in the This Workbook code in the Workbook_Open code thus:


If fm Is Nothing Then Set fm = New ufResults ' if this line was not yet passed, then a new instance of the class ufResults with the name fm is made
If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
fm.OptionButton2.Value = True

So well all sorted. Should keep me out of harms way with my project now for a while
Thanks again for all your help.

Alan

DocAElstein
12-26-2018, 04:35 PM
This is what I got. Rory did it for me
58585858https://i.postimg.cc/3dLjnXXZ/User-Form-Rory-did-for-me.jpg (https://postimg.cc/3dLjnXXZ)
https://i.postimg.cc/YC7X28jT/User-Form-Rory-did-for-me.jpg (https://postimg.cc/3dLjnXXZ)

5859https://i.postimg.cc/nCVY4RDz/User-Form-Rory-did-for-me.jpg (https://postimg.cc/nCVY4RDz)
[code][color=darkgreen]'Static fm As ufResults ' instead of this I have it as a Global variable so that i can check it elsewhere, and I add it and check a box on opening the file
'Dim fm As ufResults ' This would result in the UserForm "Dieing" a t every code ending
If Fm Is Nothing Then Set Fm = New ufResults ' if this line was not yet done, then a new instance of the class ufResults with the name fm is made
If Not Fm.Visible Then Fm.Show False 'make sure UserForm is always there,
[color=darkgreen]'Start of Part only done for entry in the three C column Ranges and no check for Calculate all else. -- Things here are that can be done quickly, 1 "row" stuff!! http://www.excelforum

DocAElstein
12-26-2018, 04:35 PM
https://www.excelforum.com/excel-programming-vba-macros/1138300-vba-userform-value-check-if-user-form-buttons-checked-not-working-check-button-on-open.html#post4384440
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24181&viewfull=1#post24181





Problem and Solution Summary
Based on the Thread Title I think a Summary could be useful for anyone coming here on a Google Search. This was a subtle problem that caught me out. Explaining it and the solution is a nice Explanation of the Basic Class, instancing thing.

First the Problem and answer, and then a bit of explanation. ( Names and the exact progression of what went on are changed a bit to protect the innocent and make this a bit clearer )

_1a ) Problem:
So I “had a UserForm.” – ( ( To “get” a UserForm ... in VB Editor .. Insert .. UserForm )
This “thing” gets a default Name, UserForm1. I see that name in a few places.
I double click on UserForm1 in the VB Editor, and without too much effort I can “ “drag” a check box into it “. “It” gets the Name CheckBox1. . I see in the VB Editor ( Alt + F11 ) the following:

460117
The above is effectively an empty Blue Print form to represent a Class of Objects. It will be “used” by VBA as a guideline for when it creates such Objects, helping it to assign memory, has code instructions for what to do in later events associated with such Objects, how to store , use them ( in this case , for example, how to “show” it ) etc. ... etc - all to be explained further......

So in my origianly problem, I tried to check in a code if the CheckBox1 check box is checked ( has a tick in it ) . I tried that with this code line, and everyone and every Google search told me it was how to do it

If UserForm1.CheckBox1.value = True Then ______

But that always gave me False.

_1b) The Solution:

Something like this did work

If fm1.CheckBox1.Value = True Then

_1c) What to do if you have this problem .. : check if your instancing of the actual userForm you are interested in has been done Explicitly, and not left to the Implicit Default

_..................

To er labberate
_2 ) What’s going in?
I got caught out because ( at least indirectly ) of the following VBA habit:
VBA tends to guess what you want when you leave things out. ( the compiler tries to determine what it is you're asking it to do ). This is good sometimes. But then relying on these Implicit defaults can “bite you when you least expect it”. In addition I fear that this results in People forgetting what VBA does . The UserForm was done for me by someone who knows what he is doing and does not rely on the Implicit defaults. I think much of what I Googled had forgot ... something here....
In all that lies the key to the problem I had


That “thing” “called” UserForm1 up there is a Class. That is to say it is a Blue Print, or a form, or a questionnaire not yet filled in, a template ... .. etc. It does not really exist in the terms of a Final product. It is just describing how something of that form or type would be.

In VBA you “Dim” things. In doing so you usually give:
a)
Variable : , a name, say fm1 if we are talking about UserForm things , or ws if we are talking about Worksheet things, and
and
b )
A Type : , what sort of thing is it.

For example as in these this “pseudo” code Lines

10 Dim fm1As AnExistingBluePrint ‘ There is no Blue Print “AnExistingBluePrint” , that is just made up for now.
11 Dim ws As AnExistingFilledInBluePrint ‘ There is no Blue Print “AnExistingFilledInBluePrint” , that is just made up for now.

( This prepares memory, instructions etc... for something of that and instructs how generally to deal with it. , and allows me to use intellisense through typing a period ( . ) to get a selection to choose from the available Methods and properties of that Object . For any particular Blue Print, the memory required may not be too different for an empty or filled in Blue Print. But there could be differences, that change further, when for example, things like strings are concerned that may vary considerable in length. Part of the instructions will explain how to handle such awkward things )
_........................
For the case of Line 10, we will be filling in from scratch our Blue Print, and do not want to mess up the original so need a copy of it.
For the case of line 11, we are talking about the equivalent of like taking a UserForm Blue print and modifying it and filling it in such that it could be used to “make” a Worksheet. In such a case it is used by Excel itself to "do that" 1 - 3 times, every time I open up Excel !!
So now I need a line to either copy a fresh Blue Print for An Existing Blue Print or pass over the Existing Filled In Blue Print
Here we go

20 Set fm1 = New UserForm1
21 Set ws = Worksheets(“Sheet1”)

That’s enough background there.
_.............................

2b) Implicit defaults for UserForms.
Now, although one might want to, it is not so typical that one finally wants to “see” or “use” more than one UserForm of a particular form and layout at the same time. If I did , these would be the appropriate code lines for a UserForm like the one I made a Blue Print Copy for above

10 Dim fm1 As UserForm1
11 Dim fm2 As UserForm1
20 Set fm1 = New UserForm1
21 Set fm2 = New UserForm1

If now I want to change any properties I can do so as in the following code line, for example, to change the state of the CheckBox1 ( say to check it - put a tick in it ), these would work, ( note each line is working on a different Object, a diffferent instance ot the Class – A differnet box is checked)

50 Let fm1.CheckBox1.Value = True
Let fm2.CheckBox1.Value = True
It would not work without lines 10 and 20. It would error , moaning that the variable fm1 had not been declared.
That sound reasonable. :rolleyes:

Because in most cases only one instance of a UserForm is used, VBA will, on any attempt to “use” something with the same name of the Class, Userform1, it will immediately effectively take code lines similar to the following as existing, ( this is the Implied default for when it first “sees” a code line attempting to use UserForm1 ):
( The two lines below is what VBA "does" effectively for you internally at Compile - you will never see these lines )
10 Dim UserForm1 As UserForm1
20 Set UserForm1 = New UserForm1

This has actually declared a new variable UserForm1, which is an instance of the Class UserForm1.
If I simply write

Let UserForm1.CheckBox1.Value = True

It will “work” as I am wanting, ( or at least as VBA is guessing I want. )

If I change the name in the of the Class userForm1 ( double click on UserForm1 in the VB Project window and edit the first row in the Properties Window, typically bottom left ) to say myFormBluePrintClass then similarly in a code I can use

Let myFormBluePrintClass.CheckBox1.Value = True

A last passing observation, to help bring out the point about the Class. The following code will give you three instances, but they will always generally be the same. They are klones. They can only be distinguished bv the Variable names given ( ot the default one discussed abobe. ). Referrencing these variables can be used then to change the properties.
But these are changes on the copy called into life by Dim Set based on / by Values of the Class UserForm1 rather than referring back to the oRefiginal.
– P-Tang Pftang OLE Wigwam Biscuit Barrel Makro Du Moley Wolly Mod Pod ; )
So they die when the UserForms are closed withh no effect on the oRefiginal Class UserForm1.
Code: (and here
http://www.excelforum.com/showthread.php?t=1101544&page=10&p=4384393&highlight=#post4384393

Sub KloneAUserFormWonks() ' ' Post #16 http://www.excelforum.com/showthread.php?t=1138300&p=4383265&highlight=#post4383265
Dim ws As Worksheet ' ' Preparing a "Pointer" to an Initial "Blue Print" ( or a Form, or a Questionnaire not yet filled in, a template etc.) in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Object of this type ) . This also us to get easily at the Methods and Properties through the applying of a period ( .Dot) ( intellisense )
Set ws = ThisWorkbook.Worksheets.Item(1) ' Values are filled at the memory locations and the directions there are specified in the variable "Blue Print "Pointer". In this case a Filled in Blue Print is passed. This is the one used on Opening by Excel to call up the user Friendly Form of the Fiist Tab Worksheet counting from the left.
Dim fm1 As UserForm1 '
Set fm1 = New UserForm1 ' ' A copy of the Blue Print is made
fm1.Left = 0
fm1.CheckBox1.Value = True
fm1.Show vbModeless ' there are two ways ( modes ) of showing a form in vb - modal and modeless. a modal form takes the focus and won't return control to the calling module until it is closed. a modeless form is shown and then control is immediately returned to the calling module. http://vbcity.com/forums/t/53342.aspx
Dim fm2 As UserForm1
Set fm2 = New UserForm1
fm2.Show vbModeless
fm2.Caption = "Klone fm2"
fm2.Left = 50
' Dim UserForm1 As UserForm1 ' These two imaginary lines are effectively done by 'The behavior (or invisible statement) serves to declare (and tell VBA to load on first use) an object variable, called UserForm1, of type UserForm1. This is called the default instance of the class (also called the "magic form" or "self/auto initiating form" by some people). This means that, while a userform operates in most ways like any other class, there is always an instance of the userform available for instant loading and use at any time. Any reference to that instance calls it into existence.
' Set UserForm1 = New UserForm1 ' VBA on compile when it sees the next line. Thes lines you would never see
UserForm1.Caption = "This one VBA decalred and Instanced for me"
UserForm1.Show vbModeless
UserForm1.Height = 50
UserForm1.Left = 200
End Sub
460261




Rem Ref http://gregmaxey.mvps.org/word_tip_pages/userforms_advanced_tips.html
Rem Ref Rory as always


_.........................................

¬_2c) Why can this catch one out, (why am I posting this “warning” )


......”........... Continued in next post..............”

DocAElstein
12-26-2018, 04:35 PM
......”........... Continued from last post..............”......


¬_2c) Why can this catch one out, (why am I posting this “warning” )

I expect many people will just take the UserForm they see as a “thing” and use it by its name, whether the default, such as UserForm1 , ( UserForm2 if they create a second User Form “thing” ) or the name they give it.
I tried to access properties through the name of the “thing” I see in the VB Editor, which was actually the Class name, as it is always is.
My Code kindly given to me by someone was more “correctly” ( IMO ) written so as not to rely on Implicit defaults, so I had something like this

10 Dim fm1 As UserForm1
20 Set fm1 = New UserForm1

Subsequently code line such as
Let UserForm1.CheckBox1.Value = True
or in particular what was causing me problems
If UserForm1.CheckBox1.Value = True Then
Did not error as the first one of those used had creating a second new instance of Class UserForm1. But I was wanting to reference the first instance, fm1, as this was used further in the code for all dealings with the actual instance of the UserForm that I was interested in.

Hope that helps anyone else Puzzled that any code line similar to this is not giving the correct results

UserForm1.Property
or
myUserFormName.Property =

_................................................. ..............



_3) The solution / answer again to the main Question


_3a) Question.
UserForm.____ not working

_3b) Answer
If you have not yourself written the existing code, then check if your instancing of the actual userForm you are interested in has been done Explicitly, rather than left to the Implicit Default, which you are ( maybe unknowingly ) assuming

Alan


Edit P.s. Tell someone you are giving such a code : “I declared Explicitly the Userform, so bear that in mind if you attempt to access any of its Properties in code modifications – I did not leave it to the Implicit default of the UserForm Class name”. ( Not that would of made any difference to me – I would of just thought – “what load of old B llox is he making up now” .. Lol.. ;) )
Rem Ref
http://www.excelforum.com/showthread.php?t=1138804&p=4383895&highlight=#post4383895
Rem Ref http://www.excelforum.com/showthread.php?t=1101544&page=9&p=4381274&highlight=#post4381274
Rem Ref http://www.excelforum.com/showthread.php?t=1101544&page=10&p=4381275&highlight=#post4381275
Rem Ref http://www.excelforum.com/showthread.php?t=1101544&page=10&p=4381420&highlight=#post4381420

DocAElstein
12-26-2018, 04:35 PM
Code for last post with a few more explaining Comments and colour conventions to suit the last Posts # 16 and #17 ( Posted here due to Forum Post size limitations: )


oRefiginal Class UserForm1.

Sub KloneAUserFormWonks() ' ' Post #16 http://www.excelforum.com/showthread.php?t=1138300&p=4383265&highlight=#post4383265
Dim ws As Worksheet ' ' Preparing a "Pointer" to an Initial "Blue Print" ( or a Form, or a Questionnaire not yet filled in, a template etc.) in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Object of this type ) . This also us to get easily at the Methods and Properties through the applying of a period ( .Dot) ( intellisense )
Set ws = ThisWorkbook.Worksheets.Item(1) ' Values are filled at the memory locations and the directions there are specified in the variable "Blue Print "Pointer". In this case a Filled in Blue Print is passed. This is the one used on Opening by Excel to call up the user Friendly Form of the Fiist Tab Worksheet counting from the left.
Dim fm1 As UserForm1 '
Set fm1 = New UserForm1 ' ' A copy of the Blue Print is made
fm1.Left = 0
fm1.CheckBox1.Value = True
fm1.Show vbModeless ' there are two ways ( modes ) of showing a form in vb - modal and modeless. a modal form takes the focus and won't return control to the calling module until it is closed. a modeless form is shown and then control is immediately returned to the calling module. http://vbcity.com/forums/t/53342.aspx
Dim fm2 As UserForm1
Set fm2 = New UserForm1
fm2.Show vbModeless
fm2.Caption = "Klone fm2"
fm2.Left = 50
' Dim UserForm1 As UserForm1 ' These two imaginary lines are effectively done by
' Set UserForm1 = New UserForm1 ' VBA on compile when it sees the next line. Thes lines you would never see
UserForm1.Caption = "This one VBA decalred and Instanced for me"
UserForm1.Show vbModeless
UserForm1.Height = 50
UserForm1.Left = 200
End Sub

DocAElstein
12-26-2018, 04:35 PM
later

DocAElstein
05-22-2024, 08:38 PM
This is post https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page2#post24185
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page2#post24185
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24185&viewfull=1#post24185
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24185&viewfull=1#post24185




An attempt at an initial summary on UserForm (module)
We are really talking about Class things and class ideas. Inevitably to get some understanding you must accept/ appreciate / be aware of, the hierarchical Object Orientated Programming concept that VBA is modelled on: Very simplified: we start, as it were towards the top, or top left, with classes and from those objects are made and they finally have Properties. The final objects with their properties ( and methods etc.), could be crudely be regarded as some "actual" or significant "things", whereas the class is just the details of how to make the objects, or a template to be used over and over again and added to or filled in appropriately to suit the particular instance like a classic simple example,
_ The concept of a car could be regarded as a Class,
_ a particular model as an instance of it,
_ and a color would be a property.
( _ A method might

The VB Editor
The VB editor is a tool we use , often referred to as the development environment, that , amongst other things, by default ,allows us to conveniently look at and develop/ change our objects. It is almost impossible not to use objects from a class if you are using Excel. By default we have limited direct access to any classes other than to make objects from them, assign variables to them, or modify the properties of the objects made from them.
But there are two sorts of Class that we can create, and we can use the VB Editor to do that as well

Use Keys Alt+F11 from Excel to get the VB Editor
, or alternatively
, right click on any worksheet tab and select view code, - https://i.postimg.cc/J04LpnsW/Right-Click-a-tab-to-show-worksheet-coding.jpg
5865
,That will show you initially a representation of the Excel objects associated with your file, which usually by default will be for the workbook itself, ThisWorkbook, and the worksheets you have. (If you took this second approach to get the VB Editor then initially the big window will be for any coding you may add for the worksheet who’s tab you selected.)
https://i.postimg.cc/nh69N8KW/Big-window-for-any-coding-you-might-want-to-add-associated-with-the-worksheet-who-s-tab-you-selected.jpg
5866
We are for now not interested in the existing things. They are associated with existing Excel objects.
It is important to realise that most object things in Excel follow the object orientated programming ideas, which for us in the current discussions means they were made from referring to some sort of blueprint / template / set of written instructions etc. We refer to these blueprint / template / set of written instructions etc. as a Class.
For most of the existing Excel objects, we have only limited, if any, direct access to the Class. That is because it will be propriety information allowing you to construct Excel, so trade secrets as it were.


We are interested in the available adding of a New Class UserForm. Associated with that UserForm Class thing is one of two class related module types that Microsoft makes available to us.
Get one with a bit of right clicking near the representation of your Excel file in the left hand VBA project window of the VB Editor,
https://i.postimg.cc/GmZzK54L/Do-a-bit-of-right-clicking-near-your-file-in-the-left-VBA-projct-explorer-and-find-the-User-Form-opti.jpg , https://i.postimg.cc/kgVfxmXY/Do-a-bit-of-right-clicking-near-your-file-in-the-left-VBA-projct-explorer-find-the-User-Form-option.jpg
5867 , 5868

You should then see the basic tools of
_ a simple pin board looking box or "form", usually with the default name of UserForm1 ,( both for the main class thing and the caption name on the pin board thing top left , ( more to that later ** ) )
, and
_ a Toolbox dialog should also be visible. (If it’s not visible select View->Toolbox from the VB editor top ribbon menu. We use the toolbox to add controls to our UserForm.)
The bottom left window should also now show the properties related to this UserForm
https://i.postimg.cc/W3Fgbm8z/User-Form-Toolbox-Properties-in-VB-Editor.jpg , https://i.postimg.cc/852WFFXH/User-Form-Toolbox-Properties-in-VB-Editor.jpg
https://i.postimg.cc/N9t5dHWv/User-Form-Toolbox-Properties-in-VB-Editor.jpg (https://postimg.cc/N9t5dHWv)https://i.postimg.cc/Q9qVwM39/User-Form-Toolbox-Properties-in-VB-Editor.jpg (https://postimg.cc/Q9qVwM39)

I will change the Name property to suit the previous discussions, to ufResults , ** , and note that the Caption did not change,
https://i.postimg.cc/FzN5szZd/Caption-name-default-User-Form1.jpg
https://i.postimg.cc/Mc0QJw2B/Caption-name-default-User-Form1.jpg (https://postimg.cc/Mc0QJw2B)
, and we will leave it like that for now, as this will help along the way, to demonstrate a quirk with UserForm Class things, which was brought out by our discussions … Unlike normal classes, UserForms are auto-instantiating, so you can simply call them by name and a new instance of the class is created. ….. as you use ufResults for the first time, a new instance of the form is created.
It's confusing because you effectively get a variable of the same name as the class.
This last statement we will come back to after getting the basics behind us now.

Important summary of what we have so far.
So we have a class now. Parts of this class is the added UserForm module. The name ufResults is effectively the name of the greater thing that is the Class. Exactly what that greater thing is, is deep in the innards of Excel and Office, it concerns how the thing eventually works , and is anyone’s guess in the meantime what / where it is. We are only interested in the available interface that we see, which we can use to determine what sort of object can be made from it. We are only concerned with tools and interface given to us to help shape how the blueprint / template / set of written instructions etc finally "looks". But note what we see and finally have is not much more significant than an instruction manual. Itself it will not "do anything". We use it to make the ( or as many as we like ) final object(s) that "does" something.
Generally a class is designed to be used over and over again, just like a template.

What is a class- what we have so far
As we said previously , it’s a blueprint / template / set of written instructions
In simple layman beginner terms its like a piece of paper in your pocket with some notes and instructions on how to build an object of this type / class. Apart from the information on it, as a thing its pretty insignificant in itself.
The whole point of the class module is just to make it more convenient to have the text / instructions / explaining diagrams etc. conveniently for you to get at in Excel and use automatically / programmatically, and thereby automatically build an object from, rather than having to painstakingly read from the bit of paper and manually construct the thing and manually add coding to it etc.
A main difference between A UserForm class module, and the other standard Class module, is that we have that main extra box with grid points on it, and other bits and pieces in the tool box, like empty lists and boxes, to make it a bit easier to fill in. In simplest layman terns, the difference between,
_ For a normal class module being like a blank piece of paper . Mainly you would put the text of coding you were interested in for you final object, on it. (Part of that typical coding text is there for you to select and add to, to save you a bit of time typing)
, and
_ The UserForm is similar, but like a bit of Graph paper along with some other pre prepared bits and pieces to stick on it. Associate with most of those things are then also available part of the text of coding that might be typically associated with those things, there for you to select and add to, to save you a bit of time typing

DocAElstein
05-22-2024, 08:38 PM
Make UserForm(s) object of the type / class ufResults

Very important to remember at this point is that we have no "thing" or object, just some notes / sketches of how we would like an object to be. We need to talk about making an object from those notes / sketches, what we refer to as something like Instantiating, instanciating, Initialising, making an object fro a class.
We won’t go here into the exact details of all the different ways to do this, but they are similar to the ideas of referring to some external libraries, as discussed here for example https://www.excelfox.com/forum/showthread.php/2240-VBA-referring-to-external-shared-Libraries-1)-Early-1-5)-Laterly-Early-and-2)-Late-Binding-Techniques?p=10568&viewfull=1#post10568
https://www.excelfox.com/forum/showthread.php/2240-VBA-referring-to-external-shared-Libraries-1)-Early-1-5)-Laterly-Early-and-2)-Late-Binding-Techniques?p=10567&viewfull=1#post10567
Strictly speaking an external Library is a package that can include amongst other things Classes to provide functionality of some sort of another. But in end effect we want the same thing: An object variable which can then be used in such a form
___= ThatLibraryObject.StufffromItIWant

Our class module so far is nothing more than a blank pop up ( a UserForm is basically a pop up , sometimes called a Form )
There is not a lot we can do with an object made from it, and an object made form it similarly can’t do much.
One thing we can do is get it to pop up. Another thing we can do is programmatically change the caption on a made object.
So this next simple coding :
_ makes an object from the class, ufResults, and the variable objufResults will be pointing to it
_ Shows that User form
_ Changes the caption to New Caption
As this is "real" coding, it can go in any existing object code module, or a normal code module. https://i.postimg.cc/nrHyDfsT/Real-coding-goes-in-any-existing-object-module-or-normal-module.jpg
(It shouldn’t go in anything to do with the class module ufResults , or any other class module, as you can not run coding from a bit of paper….. )

Option Explicit
Sub ufTest() ' https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24186&viewfull=1#post24186
Rem 1 making an instance of ufResults
Dim objufResults As ufResults
Set objufResults = New ufResults

Rem 2 The object exist, and could be considerd perhaps to have a similar status to a worksheet, although we do not have a corresponding code module for it, but we can imagine one looking like the ufResults class module
objufResults.Show vbModeless ' vbModeless means I means I can do anything outside the UserForm whilst its open, and also that the code moves on. (If I used vbModeless, then I would effectively "hang" at this code line until I closed the UserForm )
Application.Wait Time:=Now + TimeValue("00:00:03")
Rem 3 I am changing the caption on the current
Let objufResults.Caption = "New Caption" ' This changes the name in the current objufResults
Application.Wait Time:=Now + TimeValue("00:00:03")

Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
Debug.Print UserForms.Count ' 1
Unload Object:=objufResults ' close the user form instance objufResults
Debug.Print UserForms.Count ' 0
End Sub
Important to note is that after running the coding, the caption seen in the class module, ufResults is as initially. We should expect this: That caption name, UserForm1 , is what is given every time we make an instance of ufResults.
https://i.postimg.cc/gJPFGZmC/User-Form1-remains-the-caption-name-in-the-Form-diagram-in-the-class-module.jpg
5870
_._________________________

Here is the quirk ###
This is almost the same coding as the last, coding, and on running it, exactly the same thing happens

Sub ufTest2()
Rem 1 making an instance of ufResults
'Dim ufResults As ufResults
' Set ufResults = New ufResults

Rem 2 The object does not exist ?????
ufResults.Show vbModeless
Application.Wait Time:=Now + TimeValue("00:00:03")
Rem 3 I am changing the caption on the current
Let ufResults.Caption = "New Caption"
Application.Wait Time:=Now + TimeValue("00:00:03")

Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
Debug.Print UserForms.Count
Unload Object:=ufResults
Debug.Print UserForms.Count
End Sub

I have made just two main coding changes.
_ I have changed the name of the variable which was pointing to the object ,in all the corresponding places in the coding, from objufResults to ufResults, (which remains our class name as well)
, and
_ I have removed the coding that instantiates an object from the class, ( I have ' commented out Rem 1)

By the way if I put that (Rem 1 coding back in, then once again on running it, exactly the same thing happens . This latter statement is perhaps understandable, (although it is generally regarded to be unwise to use the same name for different things and can cause unexpected problems )
What is less understandable is how the coding should work fine when I appear to not instantiate the class, that is to say make an instance of the class. In most coding of this nature we would receive an error saying something like the object variable, (in this second coding ufResults, does not exist when we first try to use it , ( in this coding at code line ufResults.Show vbModeless )

So what is going on???
As Rory said…. Unlike normal classes, UserForms are auto-instantiating, so you can simply call them by (class) name and a new instance of the class is created. ….. as you use ufResults for the first time, a new instance of the form is created.
It's confusing because you effectively get a variable (pointing to the object instantiated from the class) of the same name as the class. ###

Whilst this may be considered convenient, it could easily be one reason why many people do not understand the subtle point about class, and may miss altogether that they are dealing with a class for the case of a UserForm, in particular as many people are only interested in a single instance of any particular UserForm type, and on top of this, they may not be interested particularly in changing the class name, as we did.
So they insert a UserForm module, for example a single first one. It gets the default name UserForm1. Then they go off happily in coding towards the start of the coding doing things like, pseudo coding,
UserForm1.DoSomething
They may well believe they are dealing with an object variable, UserForm1, from the start, and indeed think they have been exclusively dealing with an object, or object related things, fir example whilst working with the UserForm module and it’s various tools . But that is not the case. They where dealing with class things, and then the first time they used the word UserForm1 they effectively had this coding going on
Dim UserForm1 As UserForm1
Set UserForm1 = New UserForm1
UserForm1.DoSomething
, where UserForm1 is the class, and UserForm1 is the object

Option Explicit
Sub ufTest3() ' https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24186&viewfull=1#post24186
Rem 1 making an instance of ufResults - we can do this, or it will be done automatically by first attempt to use UserForm1 where an object is expected
'Dim UserForm1 As UserForm1
' Set UserForm1 = New UserForm1

Rem 2 The object does not exist , but will when the code line is done
UserForm1.Show vbModeless
Application.Wait Time:=Now + TimeValue("00:00:03")
Rem 3 I am changing the caption on the current
Let UserForm1.Caption = "New Caption"
Application.Wait Time:=Now + TimeValue("00:00:03")

Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
Debug.Print UserForms.Count ' 1
Unload Object:=UserForm1
Debug.Print UserForms.Count ' 0
End Sub
https://i.postimg.cc/wjNGmyb0/class-User-Form1-and-object-variable-User-Form1.jpg
5871 https://i.postimg.cc/wjNGmyb0/class-User-Form1-and-object-variable-User-Form1.jpg (https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24186&viewfull=1#post24186)

_._______


In the next post we will do just a very simple example of a userform with some coding "in it"

DocAElstein
05-22-2024, 08:39 PM
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24187&viewfull=1#post24187
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page3#post24187



Initiataelize Starting
Before dragging any template sketches from the tool box, we can take a look at what is available for coding associated primarily with the main grid, that is to say the coding "behind" the main form.
One of the first ones that is perhaps worth an initial consideration is the Initialize Event of the VBA UserForm. To get the possibility to add to the coding for that ,
_ Right-click on the main UserForm grid showing in the VB Editor, and select View Code from the menu.
_ In the Dropdown list on the left above the main Window, select UserForm.
_ This will typically give something for you to add to the UserForm_Click() event. You can ignore this.
_ In the Dropdown list on the right above the main Window, select Initialize.
(_ Optional: Delete the UserForm_Click() sub which appeared in step 2. The event coding for this and all the other available will be there anyway on an instantiated object , at this stage it is just a bit of worthless text ignored when the Class is used to make an actual object )
Remember as ever, this is all just text, which will never itself be anything of significance. It is just used later by a user New, ( or the effective automatically done one )
https://i.postimg.cc/264GZpGV/Initialize-Event-of-the-VBA-User-Form.jpg https://i.postimg.cc/05YX6SXc/Initialize-Event-VBA-User-Form.jpg
58865887

Private Sub UserForm_Initialize()

End Sub
As with all the event coding that is already available to us in VBA, this coding ios there with things in it that Microsoft prefer us not to see. This is understandable as it would be proprietary information and part of what is effectively the software Office Excel. But we can add our coding to it, for example a simple message box.

We will do this in a way to help us once again get clear the point about the possibility to neglect an instanciating,
Currently we have in the example file uploaded in the last post, two UserForm Classes, ufResults and UserForm1
We will put the same coding in both. This will be a message box to tell us the class name

Option Explicit
Private Sub UserForm_Click()

End Sub


Private Sub UserForm_Initialize()
MsgBox Prompt:="You just Initiataelized an object from the class " & Me.Name
End Sub

https://i.postimg.cc/qBK7WV5R/uf-Results-Initialize.jpg https://i.postimg.cc/8PVCSK2b/User-Form1-Initialize.jpg
58895890
That coding will tell me the class name of the particular UserForm being "made"
Now we will do the coding to Instanciate, that is to say, "make" as it were, an object of each class. Remember this is generally what happens by the New[/color] bit of the typical early declaration coding at the start of a coding to use the UserForm who’s class we made. We want to do this in two different ways, so as to demonstrate once again the confusing phenomena of a UserForm self instantiating if you neglect to do it.
These next codings can go inside any of the existing default modules.

Option Explicit ' https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24187&viewfull=1#post24187
Sub Instanciate_ufResults()
Dim Fm As ufResults
Set Fm = New ufResults ' Fm becomes the object variable pointing to an actual "thing" or object which the New efferctively caused to be "made"
End Sub
Sub Instanciate_UserForm1()
MsgBox prompt:=UserForm1.Visible ' This code bit [color=Black]=UserForm1.Visible effectively does the followoing
' UserForm1. ' This effectively causes the next two lines to be done internally as it were
' Dim UserForm1 As UserForm1
' Set UserForm1 = New UserForm1 ' UserForm1 becomes the object variable pointing to an actual "thing" or object which the New effectively caused to be "made" from using the class UserForm1
' MsgBox prompt:=UserForm1.Visible ' (This should return False because the form , is not visible)
End Sub

DocAElstein
05-22-2024, 08:39 PM
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page3
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page3#post24224
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24224&viewfull=1#post24224



A few random UserForm Examples,
using coding behind Form things

For both the main UserForm grid and anything we may, (from the tool box**) put on it, we have available coding "behind" it. This is mainly in the form of being able to tap into, that is to say, add to coding that is done when certain things take place. Usually this sort of coding is referred to as event coding.
**Note as ever, we are not talking about anything that could be regarded as things or objects here, - even if we appear to insert from, or drag things from, to the main grid from the tool box, because these are really notes / sketches / templates from which the more substantial "thing" / object will be made by the New in the typical initial instanciating steps such as
Rem 1 making an instance of ufResults
Dim objufResults As ufResults
Set objufResults = New ufResults

We remember also the confusing complicating for the class UserForm that it is self instantiating, meaning that if we missed out that initial coding section, then it would effectively be done for us, automatically, the first time we tried to use our ufResults, pseudo like
Dim ufResults As ufResults
Set ufResults = New ufResults
, meaning effectively we have an object variable which has the same name as the class from which it comes. – Very annoyingly confusing. Note, of course, that I have made the object purple just for ease of explanation. In the VB editor it will be black, and will be exactly the same word as the ufResults of the Class, as also appearing, for example, on the Class module
https://i.postimg.cc/8PtpzHW6/uf-Results.jpg
5885 https://i.postimg.cc/8PtpzHW6/uf-Results.jpg (https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page3#post24224)

DocAElstein
05-24-2024, 03:00 AM
gjhg

DocAElstein
05-24-2024, 03:00 AM
gjhg

DocAElstein
06-01-2024, 11:04 PM
later

DocAElstein
06-01-2024, 11:04 PM
later

DocAElstein
06-08-2024, 01:22 PM
later

DocAElstein
06-08-2024, 01:22 PM
later

DocAElstein
06-08-2024, 01:22 PM
er later

DocAElstein
06-08-2024, 01:22 PM
er later