Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 14th, 2008, 01:33 AM
Registered User
 
Join Date: Mar 2008
Location: Fishers, Indiana, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Control Arrays in VBA?

I understand it should be possible to create a control array in VB6 to make changes to multiple controls on a single from by looping through the index. However, I'm using Excel VBA, and I have simple changes to a form based upon choices in a combo box, and I'm not able to find a way to loop through multiple text boxes or labels to make changes without writing code over and over again (ugh!) This should be simple, maybe I just need some syntax help. . .here's what I have:
Ex:
Private Sub CmbBox1_Change()
    Select Case CmbBox1.Value
        Case "Spread"
        TxtBox6.Enabled = True
        TxtBox6.BackColor = QBColor(15)
        TxtBox7.Enabled = True
        TxtBox7.BackColor = QBColor(15)
        TxtBox8.Enabled = True
        TxtBox8.BackColor = QBColor(15)
        TxtBox9.Enabled = True
        TxtBox9.BackColor = QBColor(15)
        TxtBox10.Enabled = True
        TxtBox10.BackColor = QBColor(15)
    End Select
End Sub

As you can see, the text box are named sequentially. Shouldn't it be fairly easy to loop through the text box names by incrementing a variable and adding it to the name? i.e.
Dim inti As Integer
For i 6 To 10
 TxtBox[i].Enabled = True
 TxtBox[i].BackColor = QBColor(15)
Next i

This would make sense to me, but each time I try this method I continue to get "Invalid Qualifier". Any suggestions? As an added item, I have not been able to implement any control arrays, and have no experience doing so. If I can create control arrays, that would be great! Any help is appreciated!

Reply With Quote
  #2 (permalink)  
Old March 14th, 2008, 08:06 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

You can try the following


For Each ctl In UserForm1.Controls
    If TypeName(ctl) = "TextBox" Then
        ctl.Enabled = True
        ctl.BackColor = QBColor(15)
    End If

Next ctl


http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #3 (permalink)  
Old March 15th, 2008, 02:26 AM
Registered User
 
Join Date: Mar 2008
Location: Fishers, Indiana, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This method does work! Thanks so much, but I'm having trouble with one item. Is there any way for me to use this code and have the ability to choose which controls the changes apply to? For instance, this code would apply the changes to all the controls chosen ( TextBox ), but I would like it to only refer to a limited number of them. Is that possible?

Reply With Quote
  #4 (permalink)  
Old March 24th, 2008, 04:20 PM
Authorized User
 
Join Date: Mar 2008
Location: Athens, , Greece.
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Sanchin
 Is there any way for me to use this code and have the ability to choose which controls the changes apply to?

Yes there is.
Code:
Dim i As Integer
For i = 6 To 10
   UserForm1.Controls("TextBox" & i).Enabled = True
   UserForm1.Controls("TextBox" & i).BackColor = QBColor(15)
Next
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays for Controls in VBA Excel rduncan1 Excel VBA 3 October 3rd, 2010 11:10 PM
Matrix multiplication in VBA using arrays NewVBA Excel VBA 1 June 26th, 2006 03:15 PM
control arrays Jayahar General .NET 0 October 3rd, 2005 12:20 PM
How many control arrays can be placed in a form ramk_1978 Beginning VB 6 3 February 21st, 2005 07:57 AM



All times are GMT -4. The time now is 06:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.