VBA - Initialize Multiple Textbox values in Userform

cjwescott1

New Member
Joined
May 20, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I would like to initialize multiple textbox values in a userform transferring data from a worksheet into the userform. I found the code below on a web search however the code doesn't work as I believe the code needs to be contained within a private sub. I would like the code to be in a public sub.

Code which doesn't work below:

dim i as integer

for i = 1 to 10
controls("TextBoxA" & i) = cells(i, 1)
controls("TextBoxB" & i) = cells(i, 2)
...
next i

I receive an error, "sub or function not defined".

Anyone's assistance on this would be much appreciated.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
One correction to above. The lines of code are actually the following:

controls("TextBoxA" & i).value = cells(i, 1)
controls("TextBoxB" & i).value = cells(i, 2)
 
Upvote 0
It looks like you are likely referring to your userform from a standard module. If so, you'll need to qualify your reference to your controls...

VBA Code:
UserForm1.Controls("TextBoxA" & i).Value = Cells(i, 1).Value

So you can do it this way...

VBA Code:
Dim i As Integer

For i = 1 To 10
    With UserForm1
        .Controls("TextBoxA" & i).Value = Cells(i, 1).Value
        .Controls("TextBoxB" & i).Value = Cells(i, 2).Value
    End With
Next i

Hope this helps!
 
Upvote 0
Domenic,

That's the ticket!
I figured it was a syntax issue, but no matter what I tried it wouldn't work.
Unfortunately this part time gig of writing some VBA for excel has its challenges.
Thanks a bunch!
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,254
Members
448,556
Latest member
peterhess2002

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