Setting Global Variables from UserForm

vikasnitk85

New Member
Joined
Dec 13, 2012
Messages
2
Hi Guys,

How to call global variable from UserForm in the module.


I have written following codes for userform.


Public AnalysisVar As String

Private Sub UserForm_Initialize()
ChartAnalysisButton.Value = True
End Sub


Private Sub AnalysisOK_Click()
If ChartAnalysisButton.Value = True Then
AnalysisVar = "Chart"
Else
AnalysisVar = "Table"
End If

AnalysisTypeForm.Hide 'To close the form after clicking OK.
End Sub

Private Sub AnalysisCancel_Click()
Unload Me
End Sub




Then I am calling this userform from the module.
Sub PrintVar()
AnalysisTypeForm.Show
MsgBox (AnalysisVar)
End Sub




But When it completes the excution of AnalysisTypeForm.Show the value of AnalysisVar become null. And I get an empty message box. Guys please help me. where i am doing wrong.
confused.gif


Thanks & Regards,
Vikas Agrawal
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Thanks a lot sir for the quick reply and great help... one more doubt... do we need to define variable in userform
 
Upvote 0
Hi,
I would be more than happy if you could help me.

I have to run a macro where, at a certain point, I ask the user to set the variables "capital" and "time".
Here the code for the Userform:

Private Sub CommandButton1_Click()

capital = UserForm1.TextBox1.Value
temp = UserForm1.TextBox2.Value

Me.Hide

End Sub


In the main code of the macro I have just written:

UserForm1.Show

But is not working. VBA can't read the variables' values, so the routine does not work.
I really do not know what to do.
I am stuck on this stupid thing and cannot go ahead.
If you could help me I would be pleased.
Thank you!
D
 
Upvote 0
This could be typo error where in your code you have mentioned "temp" instead of "time".

Also if you are saving this variable value and calling it in a module then you should declare the "Capital" and "Time" as global in your module.

Just insert a module (or exisiting module) then declare as follows.

Public capital As (Data_Type)
Public Time As (Data_Type)

Data type could be string , Character , Long , Integer, decimal etc etc etc depending upon the nature of value.
 
Upvote 0
Many Thanks vds1!!
Regarding the typo error, I just wrote it wrong here (Thanks anyway).

I do not have any module created. So, should I create a module? Is not enough that I call the userform in the main code??
So are you telling me to create a module like this:

Public capital As (Data_Type)
Public Time As (Data_Type)

Private Sub CommandButton1_Click()

capital = UserForm1.TextBox1.Value
temp = UserForm1.TextBox2.Value

Me.Hide

End Sub

Than you so much
D


 
Upvote 0
create a module with only
Code:
[I][COLOR=#333333]Public capital As string[/COLOR]
[/I][COLOR=#333333][I]Public Time As string[/I][/COLOR]
otherwise, without creating module, declare variables before the sub
Code:
[I][COLOR=#333333]Public capital As [/COLOR][/I][I][COLOR=#333333]string[/COLOR][/I][COLOR=#333333][I]
Public Time As string[/I]
[/COLOR][I]Private Sub CommandButton1_Click()
[I]capital = UserForm1.TextBox1.Value
[I]temp = UserForm1.TextBox2.Value
[I]Me.Hide
[I]End Sub[/I][/I][/I][/I][/I]
 
Last edited:
Upvote 0
Hi Patel,
Thanks for the reply.

I tried as you told me, but it is still NOT working.
Is it correct that in the main code I just write :

UserForm1.Show

and nothing more???
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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