Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2008, 01:33 AM
Registered User
 
Join Date: Mar 2008
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!

 
Old March 14th, 2008, 08:06 AM
Friend of Wrox
 
Join Date: Sep 2005
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)
 
Old March 15th, 2008, 02:26 AM
Registered User
 
Join Date: Mar 2008
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?

 
Old March 24th, 2008, 04:20 PM
Authorized User
 
Join Date: Mar 2008
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





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





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