|
-
Jan 15th, 2007, 07:56 AM
#1
Thread Starter
New Member
[RESOLVED] ComboBoxes - How to disable dynamically
Hi,
I'm working with Excel 2003 VBA. I have a UserForm with 10 ComboBoxes.
And I have an general input. The ComBoboxes are filled dynamically , they have more or less items depending on this input.
When this input is less than a defined value, I disable e.g. ComboBox10. and so on. Below is part of the code (how I fill and how I disable).
I would like to know an efficient way to write a less code as possible when I need to disable several ComboBoxes.
Code used to fill part of them using an array:
VB Code:
With ComboBox2
For index = 1 To Num
.AddItem RackID(index)
Next index
End With
With ComboBox3
For index = 1 To Num
.AddItem RackID(index)
Next index
End With
etc etc....
Code used to disable them:
VB Code:
If Num = 5 Then 'MyCell8 = 40000
With ComboBox6
.Enabled = False
.BackColor = &H8000000F
End With
With ComboBox7
.Enabled = False
.BackColor = &H8000000F
End With
With ComboBox8
.Enabled = False
.BackColor = &H8000000F
End With
With ComboBox9
.Enabled = False
.BackColor = &H8000000F
End With
With ComboBox10
.Enabled = False
.BackColor = &H8000000F
End With
End If
I can say that Im a newbie. Thanks for your help,
Mafe
-
Jan 15th, 2007, 08:01 AM
#2
Re: ComboBoxes - How to disable dynamically
Moved to Office Development
-
Jan 15th, 2007, 10:37 AM
#3
Re: ComboBoxes - How to disable dynamically
Make the comboboxes an array, then you can loop to disable them
VB Code:
If Num = 5 Then 'MyCell8 = 40000
For i = 6 to 10
ComboBox(i).Enabled = False
ComboBox(i).BackColor = &H8000000F
Next i
End If
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jan 16th, 2007, 04:59 PM
#4
Thread Starter
New Member
Re: ComboBoxes - How to disable dynamically
Hi,
Thanks for your help. I have just tried this Combobox array and defined it when initializing the userform this way:
VB Code:
Dim ComboBox (6 to 10) As ComboBox
And then applied your code for that specific ComboBoxes. But I get the following error:
Run time error '91'
Object variable or With block variable not set
I tried to use the Set statement as follows:
VB Code:
For i= 6 to 10
Set ComboBox(i).Enabled = False
Next i
But then I get
Compile error: Invalid use of property.
Have I definied the wrong way the array? Mafe
-
Jan 16th, 2007, 05:27 PM
#5
Re: ComboBoxes - How to disable dynamically
Hi
This is the way you can create control array at runtime.
and then you can the use Al42's code in post#3 to disable them...
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2007, 02:43 AM
#6
Thread Starter
New Member
Re: ComboBoxes - How to disable dynamically
Hi again,
Thanks for the link. I have read it thouroughly and tried the second method. And I have defined my Object variable as follows:
VB Code:
Dim RackID() As String
Dim CboName(1 To 10) As Control
D = vbKeyA
For index = 1 To Num
RackID(index) = MyChar & MyChar1
Debug.Print index, RackID(index)
ID = ID + 1
MyChar1 = String(1, ID)
Next index
Set CboName(1) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox1", UserForm1)
Set CboName(2) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox2", UserForm1)
Set CboName(3) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox3", UserForm1)
Set CboName(4) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox4", UserForm1)
Set CboName(5) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox5", UserForm1)
Set CboName(6) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox6", UserForm1)
Set CboName(7) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox7", UserForm1)
Set CboName(8) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox8", UserForm1)
Set CboName(9) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox9", UserForm1)
Set CboName(10) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox10", UserForm1)
For index = 1 To 10
With CboName(index)
For index1 = 1 To Num
.AddItem RackID(index)
Next index1
End With
Next index
CboName(1).Font.Size = 8
CboName(1).ListIndex = 0
According to the link, it should be VB instead of MSForms, see below.
VB Code:
Set CboName(10) = UserForm1.Controls.Add("[B]VB[/B].ComboBox", "ComboBox10", UserForm1)
But, when i look the object browser this is the only library where I find ComboBox or Controls.
And I get an error:
Run time error:'-2147221005 (800401f3)':
Invalid class string
I try to load with string items that are within RackID() array.
Thanks for your help !! Mafe
-
Jan 19th, 2007, 05:02 PM
#7
Thread Starter
New Member
Re: ComboBoxes - How to disable dynamically
Just the way I wrote the solution for those that are still interested. For Excel 2003.
The important was how to write the correct Set statement.
VB Code:
Dim CboName(1 to 10) As ComboBox
Set CboName(1) = UserForm1.ComboBox1
Set CboName(2) = UserForm1.ComboBox2
Set CboName(3) = UserForm1.ComboBox3
Set CboName(4) = UserForm1.ComboBox4
Set CboName(5) = UserForm1.ComboBox5
Set CboName(6) = UserForm1.ComboBox6
Set CboName(7) = UserForm1.ComboBox7
Set CboName(8) = UserForm1.ComboBox8
Set CboName(9) = UserForm1.ComboBox9
Set CboName(10) = UserForm1.ComboBox10
'Add items to ComboBoxes
For index = 1 To 10
With CboName(index)
For index1 = 1 To Num
.AddItem RackID(index1)
Next index1
End With
Next index
If Num = 5 Then 'MyCell8 = 40000
For index = 6 To 10
With CboName(index)
.Enabled = False
.BackColor = &H8000000F
End With
Next index
Thanks for all help!! Sorry for late replies!! Mafe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|