Home > Enterprise >  Combobox additem with multiple Public Constant variable incremental
Combobox additem with multiple Public Constant variable incremental

Time:10-02

I'm trying a loop to additem in a combobox, because I have a lot of items.

I would like to make something like that :

I have Module1 with Public Constant (0 to 1024) :

Public Const Command0 As String = "1-Enable I/O (1)"  
Public Const Command1 As String = "2-Enable SNMP GET (119)"  
Public Const Command2 As String = "3-Disable I/O (2)"  
Public Const Command3 As String = "4-Disable SNMP GET (219)"
.
.
.

I want to use it with 4 Forms, so I don't want to cut and paste 1024 .additem for each (if it's possible)

I tried this, but the result is Command0, Command1,..., Command1024 etc:

Dim Str As String    
    For i = 0 To 1024    
    Str = "Command" & i      
    ComboBoxCommand.AddItem Str
    Next

How can I put an incremental variable after .additem ?

Thank you very much and I hope my english is OK... it's not my first language :-)

CodePudding user response:

Thank you,

while I'm waiting an answer, I made tests and I think I found a solution.

In Module1 I created :

Sub PopulateComboBox(ObjetName As Object)
    ObjetName.AddItem "1-Enable I/O (1)"
    ObjetName.AddItem "2-Enable SNMP GET (119)"
    ObjetName.AddItem "3-Disable I/O (2)"
    ObjetName.AddItem "4-Disable SNMP GET (219)"
End Sub

I call PopulateComboBox. I don't know if it's the better solution, but it work :-)

CodePudding user response:

As much as I love VBA, it doesn't make sense to hardcode 1024 constants into VBA when it's so easy with Excel.

Put your values on a worksheet and give the values a named range. I used "ItemsList". Then populating a ListBox is as easy as:

Combobox1.List = Sheet1.Range("ItemsList").Value
  • Related