473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to add objects into and array

I'm using VBA in excell 2007. I have a userform with 42 butons with their names ranging from dy1 to dy42. Is there a way to put these objects into an array without listing each one? In other words I would like to use the for...next loop to add them incrementally if possible. This way I can call which ever button I need when I need it. Previously I was incrementing a string and then calling the button with a select case function, and i've just discoverd how versatile arrays are but I'm still trying to learn them.

If you could help,
Thnx
Nov 4 '10 #1

✓ answered by Guido Geurs

I hope this will help:
Expand|Select|Wrap|Line Numbers
  1. 'declaration
  2. Dim button(1 To 6) As Object
  3.  
  4. 'code
  5. Private Sub UserForm_Initialize()
  6. Dim d As Integer
  7.    For d = 1 To 6
  8.       Set button(d) = Controls("commandbutton" & d)
  9.       button(d).BackColor = &H8011EE
  10.    Next
  11. End Sub
  12.  

4 4011
Guido Geurs
767 Expert 512MB
Is it possible to attach Your XLS in Bytes?
So I can better understand what You exactly want and we will work on the same data!
Nov 4 '10 #2
Here is a small example:

Expand|Select|Wrap|Line Numbers
  1. 'declaration
  2. Dim button(1 To 6) As Object
  3.  
  4. 'code
  5. Private Sub UserForm_Initialize()
  6. Dim d As Integer
  7.  
  8. Set button(1) = CommandButton1
  9. Set button(2) = CommandButton2
  10. Set button(3) = CommandButton3
  11. Set button(4) = CommandButton4
  12. Set button(5) = CommandButton5
  13. Set button(6) = CommandButton6
  14.  
  15. For d = 1 To 6
  16. button(d).BackColor = &H8011EE
  17. Next
  18. End Sub
  19.  
,but what i'd really like is something like this;

Expand|Select|Wrap|Line Numbers
  1. 'declaration
  2. Dim button(1 To 6) As Object
  3.  
  4. 'code
  5. Private Sub UserForm_Initialize()
  6. Dim d As Integer
  7.  
  8. For i = 1 To 6
  9.     Set button(i) = CommandButton(i)
  10. Next
  11.  
  12. For d = 1 To 6
  13. button(d).BackColor = &H8011EE
  14. Next
  15. End Sub
  16.  
I'm not sure how or if it's possible since and object isn't a string and that's the hurdle i'm trying to jump.
Nov 4 '10 #3
Guido Geurs
767 Expert 512MB
I hope this will help:
Expand|Select|Wrap|Line Numbers
  1. 'declaration
  2. Dim button(1 To 6) As Object
  3.  
  4. 'code
  5. Private Sub UserForm_Initialize()
  6. Dim d As Integer
  7.    For d = 1 To 6
  8.       Set button(d) = Controls("commandbutton" & d)
  9.       button(d).BackColor = &H8011EE
  10.    Next
  11. End Sub
  12.  
Nov 5 '10 #4
You are soooo awsome!! :-) Worked perfectly, Thanks.
Nov 5 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Michael Winter | last post by:
This is in IE 6... I'm trying to inherit from built-in JS objects - Array, specifically. However, I'm having some difficulties. I can add array elements to the child object, and when I retrieve...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
12
by: Andy | last post by:
Pardon the question from a beginner, but I can't seem to find the best way to do this. I have a large array and I would like to find all elements within it that fall between two values. For...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
6
by: Jeff | last post by:
Dear experts! ..NET 2.0 I'm trying to make an array containg multiple datatypes. This array will consist of 3 items (string, string, integer): my first try was this, (of course it fails)...
15
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.