Results 1 to 7 of 7

Thread: [RESOLVED] ComboBoxes - How to disable dynamically

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    9

    Resolved [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:
    1. With ComboBox2
    2.                   For index = 1 To Num
    3.                            .AddItem RackID(index)
    4.                   Next index
    5.          End With
    6.        With ComboBox3
    7.                   For index = 1 To Num
    8.                            .AddItem RackID(index)
    9.                   Next index
    10.          End With
    etc etc....
    Code used to disable them:
    VB Code:
    1. If Num = 5 Then 'MyCell8 = 40000
    2.                   With ComboBox6
    3.                            .Enabled = False
    4.                            .BackColor = &H8000000F
    5.                   End With
    6.                   With ComboBox7
    7.                            .Enabled = False
    8.                            .BackColor = &H8000000F
    9.                   End With
    10.                   With ComboBox8
    11.                            .Enabled = False
    12.                            .BackColor = &H8000000F
    13.                   End With
    14.                   With ComboBox9
    15.                            .Enabled = False
    16.                            .BackColor = &H8000000F
    17.                   End With
    18.                   With ComboBox10
    19.                            .Enabled = False
    20.                            .BackColor = &H8000000F
    21.                   End With
    22.          End If
    I can say that Im a newbie. Thanks for your help,
    Mafe

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ComboBoxes - How to disable dynamically

    Moved to Office Development

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: ComboBoxes - How to disable dynamically

    Make the comboboxes an array, then you can loop to disable them
    VB Code:
    1. If Num = 5 Then 'MyCell8 = 40000
    2.   For i = 6 to 10
    3.     ComboBox(i).Enabled = False
    4.     ComboBox(i).BackColor = &H8000000F
    5.   Next i
    6. 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

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    9

    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:
    1. 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:
    1. For i= 6 to 10
    2.    Set ComboBox(i).Enabled = False
    3. Next i

    But then I get
    Compile error: Invalid use of property.

    Have I definied the wrong way the array? Mafe

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    9

    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:
    1. Dim RackID() As String
    2. Dim CboName(1 To 10) As Control
    3.  
    4. D = vbKeyA
    5.          For index = 1 To Num
    6.                   RackID(index) = MyChar & MyChar1
    7.                   Debug.Print index, RackID(index)
    8.                   ID = ID + 1
    9.                   MyChar1 = String(1, ID)
    10.          Next index
    11.          Set CboName(1) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox1", UserForm1)
    12.          Set CboName(2) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox2", UserForm1)
    13.          Set CboName(3) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox3", UserForm1)
    14.          Set CboName(4) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox4", UserForm1)
    15.          Set CboName(5) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox5", UserForm1)
    16.          Set CboName(6) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox6", UserForm1)
    17.          Set CboName(7) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox7", UserForm1)
    18.          Set CboName(8) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox8", UserForm1)
    19.          Set CboName(9) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox9", UserForm1)
    20.          Set CboName(10) = UserForm1.Controls.Add("MSForms.ComboBox", "ComboBox10", UserForm1)
    21.  
    22.     For index = 1 To 10
    23.                   With CboName(index)
    24.                            For index1 = 1 To Num
    25.                                     .AddItem RackID(index)
    26.                            Next index1
    27.                   End With
    28.          Next index
    29.          CboName(1).Font.Size = 8
    30.          CboName(1).ListIndex = 0

    According to the link, it should be VB instead of MSForms, see below.
    VB Code:
    1. 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

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    9

    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:
    1. Dim CboName(1 to 10) As ComboBox
    2.  
    3. Set CboName(1) = UserForm1.ComboBox1
    4. Set CboName(2) = UserForm1.ComboBox2
    5. Set CboName(3) = UserForm1.ComboBox3
    6. Set CboName(4) = UserForm1.ComboBox4
    7. Set CboName(5) = UserForm1.ComboBox5
    8. Set CboName(6) = UserForm1.ComboBox6
    9. Set CboName(7) = UserForm1.ComboBox7
    10. Set CboName(8) = UserForm1.ComboBox8
    11. Set CboName(9) = UserForm1.ComboBox9
    12. Set CboName(10) = UserForm1.ComboBox10
    13. 'Add items to ComboBoxes
    14. For index = 1 To 10
    15.                   With CboName(index)
    16.                            For index1 = 1 To Num
    17.                                     .AddItem RackID(index1)
    18.                            Next index1
    19.                   End With
    20.  Next index
    21. If Num = 5 Then 'MyCell8 = 40000
    22. For index = 6 To 10
    23.                   With CboName(index)
    24.                         .Enabled = False
    25.                         .BackColor = &H8000000F
    26.                   End With
    27.  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
  •  



Click Here to Expand Forum to Full Width