Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how can I set the index of textboxes with vba

Posted on 2013-06-25
11
Medium Priority
?
457 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
  • 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 49

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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

885 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