Solved

how can I set the index of textboxes with vba

Posted on 2013-06-25
11
372 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 45

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 47

Expert Comment

by:Dale Fye (Access MVP)
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
 
LVL 45

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 45

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 45

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 45

Accepted Solution

by:
Martin Liss earned 500 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 45

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now