Solved

how can I set the index of textboxes with vba

Posted on 2013-06-25
11
420 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 47

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 (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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 47

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 47

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 47

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 47

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 47

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

752 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