MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2002, 05:15 PM   #1
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

This morning I learned from Juan Pablo G. that you cannot create a control array in VBA like you can in VB. However, there must be a way to do it. For instance, let's say I want 10 text boxes called txtTextBox(0) through txtTextBox (9). Is there a way to do this? I know there must be but I haven't been able to figure it out. Anybody out there know?

Dave
davers5 is offline   Reply With Quote
Old Mar 28th, 2002, 05:25 PM   #2
Russell Hauf
MrExcel MVP
 
Russell Hauf's Avatar
 
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
Default

There is. Are you creating these at run-time, or are you wanting to put existing controls into an array?
Russell Hauf is offline   Reply With Quote
Old Mar 28th, 2002, 05:30 PM   #3
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Can you create them at design time? If not, I'd want to create them at run time. Then again, I'd be interested to know how you'd put existing controls in an array, too.
davers5 is offline   Reply With Quote
Old Mar 28th, 2002, 05:43 PM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

I guess if you're going to do it, either at run time or design time, you're going to have to create your own "collection" object to hold the control array. You'll probably have two a class modules as well. One to define the the control and assign handle the events and the other the handle the collection. i.e. add/remove controls etc etc
Mark O'Brien is offline   Reply With Quote
Old Mar 28th, 2002, 05:45 PM   #5
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi David,

Yes, you can create an array of TextBoxes. But you didn't mention what type of textbox you were referring to, so I will assume that you meant the ActiveX TextBox control that is on the Controls toolbar for use on userforms. There are also TextBox drawing objects (actually Shape objects), and these can be put into arrays using the same approach.

Here's an example:

Dim TBarray(1 To 10) As Control
Set TBarray(1) = TextBox1
Set TBarray(2) = TextBox2
etc.

If the textboxes are all named TextBoxN (the default naming) then you could use a loop to assign all the textboxes to the array:

Dim TBarray(1 To 10) As Control
Dim i As Integer
For i = 1 To 10
Set TBarray(i) = Controls("TextBox" & i)
Next i

And of course you can use the elements of the TBarray as if they were the TextBox. For example, to set the text of the third textbox:

TBarray(3).Text = "Your Name"
__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Mar 28th, 2002, 06:17 PM   #6
Russell Hauf
MrExcel MVP
 
Russell Hauf's Avatar
 
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
Default

I guess I should have asked what you want to do with the array. I also think I spoke too soon because this is very difficult.

It involves creating a dynamic array that gets filled when the form loads, and also a Class module. Basically you loop through controls of the form as it opens (initialize event), setting each control of that type (the type you desire) to your array.

I suggest John Walkenbach's Power Programming book. Sorry if I got your hopes up by answering your post so fast and then left you out to dry.

-rh
Russell Hauf is offline   Reply With Quote
Old Apr 1st, 2002, 12:55 PM   #7
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Hello all!

When Juan Pablo G. informed me that you couldn't create a control array in the same way you did in VB I was just interested in finding out how to do it. I'm not actually using it for anything. Now, Damon's code helped me out a lot. So I've just successfully created a text box array on a userform with this code:

Private Sub UserForm_initialize()
Dim TBarray(0 To 5) As Control
Dim i As Integer
Dim intTop As Integer

intTop = 0
For i = 0 To 5
Set TBarray(i) = Controls.Add("Forms.TextBox.1", "TextBox" & i)
TBarray(i).Top = intTop + 20
TBarray(i).Text = "Name: " & TBarray(i).Name
intTop = intTop + 20
Next i

End Sub

The key difference in this code is the Add property of the control function. You have to use the correct control ID as the first argument in order to let VBA know what kind of control to add (see help for a list of ID names). Thanks for the help!

Dave
davers5 is offline   Reply With Quote
Old Oct 19th, 2009, 09:02 PM   #8
kazlady
New Member
 
Join Date: May 2009
Posts: 5
Default Re: control arrays (collection objects?) in VBA

Hey guys,
you hit on exactly what I am doing. I have 26 checkboxes at the top of my excel document that when selected check more checkboxes at the bottom of the spreadsheet. Ideally I would use Access fo rthis project, but my employer doesn't train or support Access so it won't be usable by anyone else. I am also new to writing code. Would the array code go into a Private Sub? To this point I have only worked with private subs, 1 for each checkbox on the page but would like to use arrays to reduce the amount of coding needed. All of the checkboxes perform the same function, just using their own properties. i.e: CheckBox1's caption is "Adventure". When I click CheckBox1 it searches the spreadsheet for channels in the adventure pack and activates them. If I use an array, I could have it so it refers back to its own caption to find out what to search for. Liek I said. I have the code working, but I am using a private sub for each of the 26 boxes. Would I put this array into the private sub or is there something else I need to know?
kazlady is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 10:35 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes