VBA Code to Add Items in ListBox Control in Userform


Using ListBox in Userform is very common. You can use ListBox.AddItem function to add items in the listbox.; however, it is little difficult to add items in Listbox when the number of columns are more than one.

Here is a sample VBA code to help you
VBA Code to Add Items in ListBox Control in Userform
'Event handler to add list item in ListBox
Private Sub cmdSubmit_Click()
    
    'Add an item in the listbox
    lstDepartment.AddItem "Item Name"
    'If listbox has more than one column then add column value
    'Column 1
    lstDepartment.Column(1, lstDepartment.ListCount - 1) = "Item " & lstDepartment.ListCount
    'Column 2
    lstDepartment.Column(2, lstDepartment.ListCount - 1) = "Item " & lstDepartment.ListCount
    
End Sub
Commonly used ListBox properties
VBA Code to Add Items in ListBox Control in Userform
  • Name: Can be used to define control name
  • ColumnCount: Number of columns required in the listbox
  • ColumnHeads: Defines whether you want to show header or not
  • ColumnWidths: To change default Column width (e.g. 100 pt; 100 pt; 50 pt)
  • Enabled: To enable or disable the control
  • Height: To change height of the control
  • Locked: To lock or unlock the control
  • MultiSelect: To control the multiple selection options
  • SpecialEffect: To change the look and feel of the control
  • TabIndex: Tab index of the control
  • Visible: To hide or unhide the control
  • Width: To change the width of the control

Leave a Reply

avatar
VBA Code to Delete File
EXCEL VBA TRICKS
VBA Code to Delete File

In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder

VBA Code to Add Border to Excel Range
EXCEL VBA TRICKS
VBA Code to Add Border to Excel Range

Through formatting the cells, you can make your Excel data more beautiful and easier to understand. If you record a macro to add borders to Excel range, you will notice that it generates 30 plus lines of code for just a small work. Here we are sharing one line of code which does the same thing.

Protect Excel Sheet for Manual Input
EXCEL VBA TRICKS
VBA Code to Protect Excel Sheet for Manual Input but Allow Programming Inputs

Did you come across any requirement where you want the user to interact with a sheet only through VBA Form? Here is a simple code which can help you.