Constant Variables

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Ok ...so I have a userForm (Form1) that contains a persons name that I would like to reference in a separate UserForm (Form2). In the separate UserForm (Form2) I need to reference this persons name many times, so I was wondering if there was a was to declare this name in the separate UserForm (Form2) as a constant. Only thing is that a constant, to the best of my knowledge, must be an expression and not a variable. Mainly, I'm trying to avoid declaring the myName variable in each Sub within Form2, which it will be needed for a ton of Sub's. Any ideas would be appreciated.

This is what I'm tying to do, but wondering if there is a better way to do this??

code for Form2:
Code:
Const myName As String = Form1.txtName.Value
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Ok ...so I have a userForm (Form1) that contains a persons name that I would like to reference in a separate UserForm (Form2). In the separate UserForm (Form2) I need to reference this persons name many times, so I was wondering if there was a was to declare this name in the separate UserForm (Form2) as a constant. Only thing is that a constant, to the best of my knowledge, must be an expression and not a variable. Mainly, I'm trying to avoid declaring the myName variable in each Sub within Form2, which it will be needed for a ton of Sub's. Any ideas would be appreciated.

This is what I'm tying to do, but wondering if there is a better way to do this??

code for Form2:
Code:
Const myName As String = Form1.txtName.Value


Does it not work as:

Public myName As String

placed in the public (standard) code module1?
 
Upvote 0
Yeah, That works but it just declares the variable. I wasn't able to figure out a way to set it once like you can with a constant. Is there a way to set a variable once and reuse it in multiple sub's?
 
Upvote 0
The UserForm is simply a special Class that is predefined by VBA. You can add properties and methods to this Class and extend it. Search for UserForm as Class or Object and see if you don't find ways to do what you want.

I personally would declare the myName as a Property in the UserForm and then set its value from the main calling procedure before displaying the form.
 
Upvote 0
Nevermind...I figured out what you mean. Set the variable up top and assign the variable with UserForm_Initialize()

Thanks!!
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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