Can I use a subscript as part of a VBA variable name ?

Hi All,

This is the scenario.

I have a User form with 6 text boxes named as follows :

teInvoiceNo1
teInvoiceNo2
.......
teInvoiceNo6

I am looping through a worksheet range and, depending on the value, populating the above text boxes until all 6 have been filled or until I come to the end of the range.

Rather than populating each text box specifically by name (eg teInvoiceNo4 = ) is it possible to create a loop using the loop index as the suffix digit of the text box name.

As an example, where i is the loop counter for the worksheet range (SalesArray), and x is the suffix digit of the 6 text box names, I tried the following...

Dim x as Long
Dim i as Long
i=1

For x = 1 to 100
      If teOrderNo = SalesOrderNo Then
                "teInvoiceNo" & i = SalesArray(x, 1)
                i=i+1
      End If
Next x

But it complained about the syntax.

Any ideas
Thanks
Toco
TocogroupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Dim x As Long
Dim i As Long
i = 1

For x = 1 To 100
      If teOrderNo = SalesOrderNo Then
                Me.Controls("teInvoiceNo" & i) = SalesArray(x, 1)
                i = i + 1
      End If
Next x

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TocogroupAuthor Commented:
Many thanks for that. A quick question...
Does 'Me.' relate to the Active worksheet in all cases ?
0
Rgonzo1971Commented:
no it depends where it is called from

if it is  called from a form, it would be the form itself
if it is called from thisWorbook module it would be thisWorbook
etc.

Regards
0
TocogroupAuthor Commented:
Thanks again
Toco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.