PC Review


Reply
Thread Tools Rate Thread

Display variable in userform

 
 
PJ
Guest
Posts: n/a
 
      11th Nov 2009
I would like to use custom userforms for various dialog boxes, error
trapping, etc. and wanted to know if it is possible to reference a string
variable on a userform. For example, say I have a variable called myVar in
my code and certain conditions are met where I want to display a userform. I
want to add a label with text on the userform and have it display the value
of myVar. Is there a way to do this?
 
Reply With Quote
 
 
 
 
Sam Wilson
Guest
Posts: n/a
 
      11th Nov 2009
Make myVar public - ie don't declare it in a sub, but before the first sub
put the line

Public myVar as variant

Then in your userform code you can refer to it.

Sam


"PJ" wrote:

> I would like to use custom userforms for various dialog boxes, error
> trapping, etc. and wanted to know if it is possible to reference a string
> variable on a userform. For example, say I have a variable called myVar in
> my code and certain conditions are met where I want to display a userform. I
> want to add a label with text on the userform and have it display the value
> of myVar. Is there a way to do this?

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      11th Nov 2009
To follow up on Sam's posting.... go into the VB editor and copy/paste
this...

"Understanding Scope and Visibility"

into the Search Box and then click on the item with the same name in the
list that appears... this will give you more details about what Sam has
suggested to you.

--
Rick (MVP - Excel)


"Sam Wilson" <(E-Mail Removed)> wrote in message
news:A4CB38AC-E2DA-4E13-8A0E-(E-Mail Removed)...
> Make myVar public - ie don't declare it in a sub, but before the first sub
> put the line
>
> Public myVar as variant
>
> Then in your userform code you can refer to it.
>
> Sam
>
>
> "PJ" wrote:
>
>> I would like to use custom userforms for various dialog boxes, error
>> trapping, etc. and wanted to know if it is possible to reference a string
>> variable on a userform. For example, say I have a variable called myVar
>> in
>> my code and certain conditions are met where I want to display a
>> userform. I
>> want to add a label with text on the userform and have it display the
>> value
>> of myVar. Is there a way to do this?


 
Reply With Quote
 
PJ
Guest
Posts: n/a
 
      12th Nov 2009
Thanks for the help, but I'm still confused.

Say my conditions are met and I have created a "message string" in my code.
So when I have the code display the userform, how do I get the string to show
on the userform? If I add a label, can I set the value to the msgVar1? If
so, how? I was trying to set the caption of the label on the userform to the
variable, but didn't have any success. I'm not sure I'm going about this the
right way.

Thanks

"Rick Rothstein" wrote:

> To follow up on Sam's posting.... go into the VB editor and copy/paste
> this...
>
> "Understanding Scope and Visibility"
>
> into the Search Box and then click on the item with the same name in the
> list that appears... this will give you more details about what Sam has
> suggested to you.
>
> --
> Rick (MVP - Excel)
>
>
> "Sam Wilson" <(E-Mail Removed)> wrote in message
> news:A4CB38AC-E2DA-4E13-8A0E-(E-Mail Removed)...
> > Make myVar public - ie don't declare it in a sub, but before the first sub
> > put the line
> >
> > Public myVar as variant
> >
> > Then in your userform code you can refer to it.
> >
> > Sam
> >
> >
> > "PJ" wrote:
> >
> >> I would like to use custom userforms for various dialog boxes, error
> >> trapping, etc. and wanted to know if it is possible to reference a string
> >> variable on a userform. For example, say I have a variable called myVar
> >> in
> >> my code and certain conditions are met where I want to display a
> >> userform. I
> >> want to add a label with text on the userform and have it display the
> >> value
> >> of myVar. Is there a way to do this?

>
> .
>

 
Reply With Quote
 
PJ
Guest
Posts: n/a
 
      12th Nov 2009
OK I think I figured it out.

I declared "msgString1" as a public variable and set it to this in my code:
msgString1 = "Some text here..." & vbNewLine _
& vbNewLine & "And some more text here " & myVar1 & " more stuff."

Then I added a label named "msgVar1" on the userform and set it like so.
Form1.msgVar1 = msgString1
Form1.Show

It seems to work but I was hoping someone could confirm this is an
acceptable way of doing it.

Thanks - PJ
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      12th Nov 2009
> OK I think I figured it out.
>
> I declared "msgString1" as a public variable and set it to this in my
> code:
> msgString1 = "Some text here..." & vbNewLine _
> & vbNewLine & "And some more text here " & myVar1 & " more stuff."
>
> Then I added a label named "msgVar1" on the userform and set it like so.
> Form1.msgVar1 = msgString1
> Form1.Show
>
> It seems to work but I was hoping someone could confirm this is an
> acceptable way of doing it.


It is fine to do it that way. Another way would be to put the assignment
statement in the UserForm's Initialize event procedure. Doing it that way,
would automatically make the assignment when the UserForm is initially shown
(that is, after the first time it is shown in a session or the next time it
is show after having been Unload'ed) and you would not have to remember to
set it each time your code Show's the UserForm.

--
Rick (MVP - Excel)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm Variable Ronald R. Dodge, Jr. Microsoft Excel Programming 2 28th Jul 2009 06:19 PM
How to feed a userform a variable to display in a textbox? Chet Microsoft Excel Programming 2 1st Feb 2008 02:46 PM
Userform and variable =?Utf-8?B?SmVmZg==?= Microsoft Excel Misc 1 16th Mar 2007 03:14 PM
userform and variable Gary Keramidas Microsoft Excel Programming 11 6th Dec 2006 05:34 PM
VBA Variable as userform smithb2@student.gvsu.edu Microsoft Excel Programming 5 13th Apr 2006 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:15 AM.