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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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
 

Forum statistics

Threads
1,147,674
Messages
5,742,541
Members
423,736
Latest member
dracula cyrus

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
Top