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

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"

cjwescott1

New Member
Joined
May 20, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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)
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,722
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!
 

cjwescott1

New Member
Joined
May 20, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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!
 

Forum statistics

Threads
1,141,410
Messages
5,706,292
Members
421,439
Latest member
JordsdoExcel

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