Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how can I set the index of textboxes with vba

Posted on 2013-06-25
11
Medium Priority
?
445 Views
Last Modified: 2013-06-25
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?
0
Comment
Question by:Dov_B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39274817
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
 

Author Comment

by:Dov_B
ID: 39274826
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
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39274838
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 49

Expert Comment

by:Martin Liss
ID: 39274843
Which portion, top or bottom, do you get the textboxes from?

?
0
 

Author Comment

by:Dov_B
ID: 39274855
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39274861
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
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39274872
deleted
0
 

Author Comment

by:Dov_B
ID: 39274890
uh I thought textboxes were shapes like pictures
0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 39274916
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
 

Author Comment

by:Dov_B
ID: 39274928
I see what u r doing. ok thanks
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39275014
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question