variable userform object name?

mcgew

Board Regular
Joined
Sep 4, 2002
Messages
75
I have to transfer information from a number of textboxes on a userform to a spreadsheet.
Rather than list every textbox, i want to be able to loop through them to reduce the amount of code.

ie instead of:
Cells(1,1)=Textbox1.value
Cells(2,1)=Textbox2.value
Cells(3,1)=Textbox3.value
etc

I want to do something like

For i = 1 to 10
myrow = i
Cells(myrow,1)=Textbox i.value
next i

the above doesn't work - is it possible to do somehow?

Thanks,
Grace
:confused:
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi Grace,

It depends on how you have named the TextBoxes. If they just have the default names (as you seem to indicate) then try:
Code:
Private Sub CommandButton1_Click()
    Dim i As Integer
    
    For i = 1 To 3
        Sheet1.Cells(i, 1).Value = Me.Controls("TextBox" & i).Value
    Next i
    
End Sub
If they have a variety of names then you could add some sort of unique identifier in the .Tag property of each of the TextBoxes concerned and check for this as you loop through the controls on the form.

HTH
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

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