using For statement for populting textbox references

R

Roger on Excel

I use the following type of code to populate textboxes in userforms

Me.txt1.Value = ws.Range("A10").Value
Me.txt2.Value = ws.Range("A11").Value
Me.txt3.Value = ws.Range("A12").Value
Me.txt4.Value = ws.Range("A13").Value
Me.txt5.Value = ws.Range("A14").Value
Me.txt6.Value = ws.Range("A15").Value
Me.txt7.Value = ws.Range("A16").Value
Me.txt8.Value = ws.Range("A17").Value
etc etc...

Is there a way to use a "For i = 1 to 8" statement which will do the above
code : for example

Dim i as Integer
For i = 1 to 8
Me.txt & i.Value = ws.Range("A" & Cstr(i + 9))
Next i

Can anyone help with the syntax for this?

Many thanks,

Roger
 
R

Rick Rothstein

You can do it like this...

For X = 1 To 8
Me.Controls("txt" & X).Value = ws.Cells(9 + X, "D").Value
Next

If you Dim your variables (and you should), X would be a Long.
 

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