TextBox Popluation

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
Hi,

I have a list of text boxes (TextBox1 thru TextBox20) in my Userform. Is there an elegant way to populate these text boxes using sheet1, column a, rows 1 thru end of rows?

The only way I know is to move each cell's value from the source sheet to the individual textbox, i.e., TextBox1.Text = cell...., TextBox2.Text = cell...., etc.

I was thinking that there may be a way to loop thru the cells in the sheet and populate then increment the TextBox to the next.

Thanks.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
maybe:

Code:
Private Sub CommandButton1_Click()
 
For i = 1 To 20
   UserForm1.Controls.Item("TextBox" & i).Text = Sheets("Sheet1").Range("A" & i)
Next i
 
End Sub


or if you don't know how many textboxes you have or if the number of textboxes are variable then perhaps:

Code:
Private Sub CommandButton1_Click()
 
For i = 0 To UserForm1.Controls.Count - 1
 
   If UserForm1.Controls.Item(i).Name Like "TextBox*" Then
        UserForm1.Controls.Item("TextBox" & r).Text = Sheets("Sheet1").Range("A" & r)
        r = r + 1
    End If
 
Next i
 
End Sub
 
Last edited:
Upvote 0
Item("TextBox" & r

r = r + 1

What;s happening here with the use the "r"?
What's its original value?

Thanks,

Jim
 
Upvote 0
my bad. in the second code before the for loop starts there should have been an r = 1 to start the variable. I think I accidentally deleted it while editing the post:

Code:
r = 1
 
For 
    ...
Loop
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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