Using variables to loop through UF controls

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,647
I have a userform (as you might suspect from the title :) ) with several (100s) of textboxes and comboboxes that will be posted to a worksheet. I'm hoping I can loop through all the controls instead of typing out each one. I tried to use this:

Code:
For i = 1 To 6
    ActiveSheet.Range("B" & i + 8).Value = TbPh1ShotNum & i.Value
Next i

but with no success. I'm almost sure it's just a syntax problem rather than a "that's not possible" problem. Does anyone know the correct way to code this?

Thanks in advance for any help you can offer.

BTW, it's telling me that TbPh1ShotNum is not a defined variable....which is true. I thought with the "& i" behind it, it would string it together, but alas :( .
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you name (by typing the desired name into the (name) field in the properties window) your text boxes on the userform to be "Textbox1", "Textbox2", etc. you can then use:

Code:
ActiveSheet.Range("B" & i + 8).Value = UserForm1.Controls("Textbox" & i).Text

or similar
 
Upvote 0
I figured it was something simple, but yet beyond my knowledge (there's a LOT of that out there :) ) . Worked perfectly. Thank you so much.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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