Public Variables - Releasing Values

AlanAnderson

Board Regular
Joined
Jun 7, 2010
Messages
134
Hi,
Another dumb question:
I have Userform1 which obtains info which is then used by Userform 2. To achieve this I am using public variables.
What I am unsure of is how to clear the values in those variables once UserForm2 ends. Is it as simple as vPublic1="" in the last line of userform?
Is there a better way to do this?

Regards,

Alan
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
its how I do it, or if numeric, just fill it with a zero
 
Upvote 0
Pass the information to the second userform directly, either by public variables declared in that userform, or using Property routines. They will automatically go out of scope when the form is unloaded.
 
Upvote 0
Hi Mole - thanks - that's what I'm doing.

Rory, would you mind explaining what you mean by "using property routines"?
Thanks,

Alan
 
Upvote 0
Hi Rory, Further to my last mail. If I run these user forms using F5 until they end. I then click reset. If I then run a small routine with msgbox I find these Public Variables still retain the values from both Forms 1 and 2. Do I haqve to clear them by setting as = "" or 0 ?

Thanks again,

Alan
 
Upvote 0
The public variables must be declared in the userform code module, not a normal module. They will then go out of scope when the form terminates.
 
Upvote 0
For the property procedures, have a look at the Class Basics section on this page (userforms are effectively a class)
 
Upvote 0
Glad to help. As an example, this is the code I have in my template form so that all my forms have a Cancelled property (and they are always hidden, not unloaded, from the form itself - the calling code takes care of unloading them):
Code:
Option Explicit
Private m_blnCancelled As Boolean

Public Property Get Cancelled() As Boolean
    Cancelled = m_blnCancelled
End Property

Private Sub cmdCancel_Click()
    m_blnCancelled = True
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    m_blnCancelled = False
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
        cmdCancel_Click
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,941
Members
449,480
Latest member
yesitisasport

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