ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   using For statement for populting textbox references (https://www.excelbanter.com/excel-programming/437807-using-statement-populting-textbox-references.html)

Roger on Excel

using For statement for populting textbox references
 
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

Rick Rothstein

using For statement for populting textbox references
 
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.

--
Rick (MVP - Excel)


"Roger on Excel" wrote in message
...
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




All times are GMT +1. The time now is 02:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com