I get the feeling I’m opening a major can of worms for myself with this question, but…
Is it possible to pass the value of a variable from one event to another within the code for a userform?
(Does that question even make any sense?)
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Passing variables in events?
I’d pass it via variables scoped for the userform- ie, defined before any procedures:
Option Explicit Private strMyString As String Private Sub cmdCancel_Click() strMyString = "Cancel Pressed" End Sub Private Sub cmdOK_Click() MsgBox strMyString End Sub Private Sub UserForm_Initialize() strMysString = "Start Value" End Sub
>>> Option Explicit
Private strMyString As StringPrivate
Sub cmdCancel_Click()
strMyString = “Cancel Pressed”
End Sub … <<<
This led me to a solution — and a learning experience.
I was unloading the userform in the first line of my CmdOK_Click() procedure. I couldn't figure out why your example worked and my variation on it didn't, until I realized that unloading the form also wipes out the values of any variables declared in the initialization procedure.
Thanks very much for the assist!
I’ve a similar problem. I’d like to do something like:
Sub PutNumber(Number)
Range(“B3”).Select
ActiveCell.Value = Number
End Sub
Then have a button on my workbook that would be attached to a macro called PutNumber 5 for example. But this doesn’t work. Can you actually define a variable in this way? if not then what are the ()s for?
Bob
I just can’t get it to work for me and I’ve tried and tried. I am a beginner so I’ve never actually done it at all. Someone wouldn’t like (mind) puttin together an example for me would they?
eg
Sub message(text)
msgbox(text)
end sub
then place a command button on a page assigned to macro:
Message (“hello world”).
Bob
I really don’t get where your parameter value comes from. Please clarify. By page do you mean a Word document or Userform? Since you cannot pass a parameter into a clicked event, you have to compromise. A command button named Button on a Word document can be pressed to display “Hello World” But somehow I don’t think this is what you are after.
In the Thisdocument object:
Dim strMsg As String
Private Sub Button_Click()
Call message(“Hello World”)
End Sub
Sub message(strIn As String)
MsgBox strIn
End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications