Automatically Populate TextBox in UserForm

millhouse123

Active Member
Joined
Aug 22, 2006
Messages
335
I have a userform that pops up upon opening a file. The user form has
5 text boxes names Zero, Two, Four, Seven and Nine. I would like each of these text boxes to automatically populate with the values last used. The values last used are stored in Worksheet "Index Settings" in Cells "C3:C7". Any help on the coding for this would be much appreciated.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Private Sub UserForm_Initialize()
'UserForm module code!

TextBox1.Value = Sheets("Sheet1").Range("C3").Value
TextBox2.Value = Sheets("Sheet1").Range("C4").Value
TextBox3.Value = Sheets("Sheet1").Range("C5").Value
TextBox4.Value = Sheets("Sheet1").Range("C6").Value
TextBox5.Value = Sheets("Sheet1").Range("C7").Value
End Sub
 
Upvote 0
Joe

thanks for your help

I have tried the following and it gives me error "Object Required"

Zero.Value = Sheets("Index Settings").Range("C3").Value

Zero = Text box Name
 
Upvote 0
I am sorry I left out an important detail.

I have a userform that opens when I open tihs workbook. I select a single option on this userform and depending on the option chosen this second Userform pops up. This is probably causing the error however, I don't know enough about VBA to know why.
 
Upvote 0
I am sorry I left out an important detail.

I have a userform that opens when I open tihs workbook. I select a single option on this userform and depending on the option chosen this second Userform pops up. This is probably causing the error however, I don't know enough about VBA to know why.
 
Upvote 0
I am sorry I left out an important detail.

I have a userform that opens when I open tihs workbook. I select a single option on this userform and depending on the option chosen this second Userform pops up. This is probably causing the error however, I don't know enough about VBA to know why.
 
Upvote 0
After further thinking this over - I believe the problem is that I need this triggered when the patricular userform is shown. is the a userform.show event?
 
Upvote 0
Yes it is the: Private Sub UserForm_Initialize() I posted!

You should only have one UserForm per Form-Module!

You could if you are doing something not normal conscript the UserForm, like:


Private Sub UserForm_Initialize()
'UserForm module code!

UserForm2.Zero.Value = Sheets("Sheet1").Range("C3").Value

End Sub

Or


Private Sub UserForm_Initialize()
'UserForm module code!

UserForm2.Zero.Text = Sheets("Sheet1").Range("C3").Value

End Sub
 
Upvote 0
millhouse123

Can you post the exact code that's not working?

And tell us where you put it.
 
Upvote 0
Thank you so much for your patients.

below is copy of the code for the first form which when the certain index is chosen it calls the second. The Second form is the one I am trying to populate.

I am certain it is clear in your mind where to put the initialize but I am confused. Thanks again for all of your help.

Code:
Private Sub OKButton_Click()
If Index1 Then
    Worksheets("Index").Activate
    Range("A1").Activate
    ActiveCell.Offset(0, 1).Value = 1
    ActiveCell.Offset(1, 1).Value = 0
ElseIf BlendedIndecies Then
    Allocation.Show
ElseIf LehmannAgg Then
     LAAllocation.Show
        
ElseIf BondLadder Then
    Worksheets("Index").Activate
    Range("A1").Activate
    ActiveCell.Offset(0, 1).Value = "Bond Ladder"
    ActiveCell.Offset(1, 1).Value = 0
    Bondladder2.Show
End If

If OptionButton1 Then
    Worksheets("Index Settings").Range("C10").Value = "Yes"
Else
    Worksheets("Index Settings").Range("C10").Value = "No"
End If

Dim WS1 As Worksheet
Set WS1 = Worksheets("Report")
WS1.Range("E7").Value = Val(Equity)
WS1.Range("E5").Value = Val(Cash)

Unload IndexChoices
    
End Sub




Private Sub CommandButton1_Click()
Worksheets("Index").Activate
Range("A1").Activate
ActiveCell.Offset(0, 1).Value = 0#
ActiveCell.Offset(1, 1).Value = 0#

If Val(Zero) + Val(Two) + Val(Four) + Val(Seven) + Val(Nine) <> 1 Then
    MsgBox "The Allocation does not equal 100%", vbOKOnly, Error
    Exit Sub
End If

Worksheets("Index Settings").Activate
Dim WS1 As Worksheet
Set WS1 = Worksheets("Index Settings")
Finalrow1 = WS1.Cells(65536, 1).End(xlUp).Row
Range("C3") = Val(Zero)
Range("C4") = Val(Two)
Range("C5") = Val(Four)
Range("C6") = Val(Seven)
Range("C7") = Val(Nine)
Worksheets("Holdings").Activate
Unload LAAllocation

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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