Transferring Captions/Text between UserForms

G

Guest

This might be hard to explain...

I have a useform with 24 buttons and 24 textboxes. Each button corresponds
to a textbox next to it. The idea is to click a button to open up another
userform with several optionbuttons, then click the optionbutton and have the
optionbutton caption be put into the textbox that corrisponds to the button
that was originally clicked. (hope that makes sense)

I have tried assigning a variable (as String) to pass between the userforms,
but the caption gets lost (comes back blank).

(on UserForm1)
Private Sub cmdbutton_Click()
Dim myVar as String
buttonbox.Text = myVar
UserForm2.Show
End Sub

(on UserForm2)
Private Sub optionbutton1_Click()
myVar = optionbutton1.Caption
Unload Me
End Sub
 
D

Dave Peterson

One way is to declare a public variable in a General module--outside any
procedure:

Public MyVar as string

Then that variable will be able to be seen from any procedure.

=====
Alternatively, I could use a public variable in the userform1 module:

Option Explicit
Public myVar As String
Private Sub CommandButton1_Click()
UserForm2.Show
Me.TextBox1.Text = myVar
End Sub

Then I could address it this way:

Option Explicit
Private Sub optionbutton1_Click()
UserForm1.myVar = Me.OptionButton1.Caption
Unload Me
End Sub
 
G

Guest

That did the trick. Thanks for the help.

I do have a question about using myVar in a Public statement... I have 23
other buttons and text boxes that will need to perform this same function.
Can I use that same myVar variable to transfer captions back and forth?
 
D

Dave Peterson

I imagine you could, but I don't think I would.

I think I'd add a button to the second workbook that accumulated the captions to
pass back and send them back as an array.

Inside the General module:

Option Explicit
'as many textboxes and optionbuttons you have
Public myVars(1 To 23) As String


Behind Userform1:

Option Explicit
Private Sub CommandButton1_Click()
Dim iCtr As Long
UserForm2.Show
For iCtr = LBound(myVars) To UBound(myVars)
Me.Controls("textbox" & iCtr).Value = myVars(iCtr)
Next iCtr
End Sub

Behind userform2:

Option Explicit
Private Sub CommandButton1_Click()
Dim iCtr As Long
'you know how many optionbuttons you have
'and you named them nicely
'OptionButton1, OptionButton2, ...OptionButton23
For iCtr = LBound(myVars) To UBound(myVars) 'I only had 2!
With Me.Controls("OptionButton" & iCtr)
If .Value = True Then
myVars(iCtr) = .Caption
Else
myVars(iCtr) = "" 'empty string?
End If
End With
Next iCtr
Unload Me
End Sub

==========
And I'm assuming that the optionbuttons are grouped nicely--without being
grouped, you'll only be able to select a single optionbutton.

And notice that my OptionButtons and Textboxes are named nicely--so
Optionbutton17 and textbox17 will be "associated" with each other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top