UserForm Question

G

Guest

Hello
I have a created a userform and would like the values in txtBox3-txtBox10 to
default to the value in txtBox1 once it is entered. So if I enter 100 in
txtBox1 and then go to txtBox2 the values in 3-7 will be 100 also. I am new
to user forms and I dont know how to do this. All help would be greatly
appreciated!
Thanks in advance
Sharon
 
E

Ed

TextBoxes have a Change event. You might be able to use this to set
txtBox2.Text = txtBox1.Text, etc.

HTH
Ed
 
B

Bob Phillips

Private Sub TextBox1_Change()
With Me.TextBox1
TextBox2.Text = .Text
TextBox3.Text = .Text
TextBox4.Text = .Text
TextBox5.Text = .Text
TextBox6.Text = .Text
TextBox7.Text = .Text
End With
End Sub

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
T

Tom Ogilvy

Use the exit event of the Textbox1

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If trim(Textbox1.Text) <> "" then
for i = 3 to 10
me.controls("TextBox" & i).Text = Textbox1.Text
Next
End if
End Sub

If you only want this behavior if the textboxes 3 to 10 are empty

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If trim(Textbox1.Text) <> "" then
for i = 3 to 10
if trim(me.controls("Textbox" & i).Text) = "" then
me.controls("TextBox" & i).Text = Textbox1.Text
End if
Next
End if
End Sub
 
G

Guest

Thanks Bob
A followup question:
How would I modify if in textBox8 and TextBox9 the values 2 and 5
respectively for it to change textBox2 through textBox5, and not 6 and 7?
Thanks!
 
T

Tom Ogilvy

Enamored with repetitiveness you asked:
How would I modify if in textBox8 and TextBox9 the values 2 and 5
respectively for it to change textBox2 through textBox5, and not 6 and 7?

Private Sub TextBox1_Change()
With Me.TextBox1
TextBox2.Text = .Text
TextBox3.Text = .Text
TextBox4.Text = .Text
TextBox5.Text = .Text
' TextBox6.Text = .Text
' TextBox7.Text = .Text
TextBox8.Text = 2
TextBox9.Text = 5
End With
End Sub

However, using the exit event would be a better choice.
 
G

Guest

Im sorry thats not quite what I meant.....
TextBox1 user enters 100
Textbox8 user enters 2
TextBox9 user enters 5
and then form is updated TextBox2 through TextBox5 =100 TextBox6 and
TextBox7 are blank.
So the code reads TextBox8 and TextBox9 and then fills in 2-7 based on whats
entered in 8 and 9.

Sorry for the lack of clarity---it made perfect sense to me LOL
I hoep this clarifies a bit
 
T

Tom Ogilvy

All placed in the userform code module

Sub UpdateBoxes()
Dim j as Long, k as Long, i as Long
if isnumeric(me.Textbox8.Text) then _
j = clng(me.Textbox8.Text)
if isnumeric(me.Textbox9.Text) then _
k = clng(me.Textbox9.Text)
if j < 2 or j > 7 or k < 2 or k > 7 then exit sub
for i = j to k
me.Controls("TextBox" & i).Text = Textbox1.Text
Next
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
UpdateBoxes
End Sub


Private Sub TextBox8_Exit(ByVal Cancel As MSForms.ReturnBoolean)
UpdateBoxes
End Sub


Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
UpdateBoxes
End Sub
 

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