how can I set the index of textboxes with vba

I deal with a lot of textboxes and move them around a great deal with vba but is there a way to name them or set there index so I can refer to them in code?
Dov_BAsked:
Who is Participating?
 
Martin LissOlder than dirtCommented:
They are. I was wrong but you can still do this.


Option Explicit
Private Enum TextBoxNames
    FirstName = 1 ' Note that a starting value is required for VBA
    MiddleInitial
    LastName
End Enum
Sub Macro10()
'
ActiveSheet.Shapes(LastName).TextFrame.Characters.Text = "Smith"
End Sub

Open in new window

0
 
Martin LissOlder than dirtCommented:
Unfortunately VBA does not have the "control array" feature of VB6 so there's no direct way referring to an Index value. Are the textboxes you are using on a UserForm? If they're on a sheet are they ActiveX controls or form controls?
0
 
Dov_BAuthor Commented:
I am not sure what there official name is but I am talking about textboxes you get by going to the insert tab on the ribbon and inserting a textbox that floats abov the spreadsheet
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Dale FyeCommented:
best way is to name the textboxes something like: txt_1, txt_2, txt_3

Then you can refer to them using the controls collection:

controls("txt_" & intValue)
0
 
Martin LissOlder than dirtCommented:
Which portion, top or bottom, do you get the textboxes from?

?
0
 
Dov_BAuthor Commented:
0
 
Martin LissOlder than dirtCommented:
To build on fyed'd solution you can add something like this.

Private Enum TextBoxNames
    FirstName 
    MiddleInitial 
    LastName 
End Enum

Open in new window


and then if you want to refer to the LastName textbox you can do

controls("txt_" & LastName)  

since Enum values are automatically given integer values starting with 1 so  LastName will be 3.

If your names are not in order line txt_1, txt_2, etc., no problem just do

Private Enum TextBoxNames
    FirstName = 4
    MiddleInitial = 7
    LastName = 23
End Enum

Open in new window

0
 
Martin LissOlder than dirtCommented:
deleted
0
 
Dov_BAuthor Commented:
uh I thought textboxes were shapes like pictures
0
 
Dov_BAuthor Commented:
I see what u r doing. ok thanks
0
 
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.