VBA forms in excel

T

tcassio

I use forms for data input in excel. I have a for named test. I have 10
fields on the form named t1 thru t10. Currently when I open the form I get
data from a sheet and put it in the field names using:
test.t1.value=sheetname.range("H8"). using this method I have to create 10
lines of code to populate the ten fields. How can I use a for next loop to
change the field name in test.t1.value while stepping through the loop ie;
t1, t2, t3 ect.
 
D

Dave Peterson

You didn't share how you loop through the cells. So I used A1 to A3 in my
tests.

You'll have to change the name of the textbox, too.

Option Explicit
Private Sub UserForm_Initialize()
Dim iCtr As Long

For iCtr = 1 To 3 '10 for you
Me.Controls("textbox" & iCtr).Value _
= ActiveSheet.Cells(iCtr, "A").Text
Next iCtr

End Sub
 
J

JLGWhiz

By Field, do you mean Text Box, Label, List Box, ComboBox ? Not familiar
with what a field is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top