Listbox in UserForm

vijay2482

Board Regular
Joined
Mar 3, 2009
Messages
142
hi all,

i have a ListBox in my userform
i want to enter n items in the listbox at runtime
i also want to have a delete button,so that if i think i dont want that particular item in my listbox,by selecting that item from that listbox and clicking delete,should remove/delete that item from the listbox.

is it possible to do?

any help is appreciated

thanks in advance
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Yes that's all possible and fairly easy to accomplish. Only slightly more complicated if you wish to add values to a multi column list box.

(Assuming its called ListBox1)

Adding items is the same as adding them to a Combobox i.e

Code:
Me.ListBox1.Additem(itemvalue)
To delete the selected item your command button code should contain

Code:
'Exits without action if no item is selected
If Me.ListBox1.ListIndex=-1 then Exit Sub

Me.ListBox1.RemoveItem(Me.ListBox1.ListIndex)
 
Upvote 0
thanks for the reply

the values should be displayed in the listbox
and from the list item can be selected to delete
 
Upvote 0
Hi, With a ListBox and Delete button on your Userform, you could use this:-
Code (1) to load the ListBox and code (2) to delete item seleted.
Code:
Private Sub UserForm_Initialize()
Dim Rng As Range
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
ListBox1.List = Rng.Value
End Sub
Code:
Private Sub CommandButton1_Click()
Dim Del As Integer
For Del = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(Del) Then
        ListBox1.RemoveItem ListBox1.ListIndex
    End If
Next Del
End Sub
Regards Mick
 
Upvote 0
i tried your first code, it worked perfect with a range in the excel sheet
but i dont have a range

i want the items to be entered directly in the listbox one after the otherduring runtime
 
Upvote 0
Hi, Where are you getting the ListBox items from ??
Off the sheet or hard coded in code !!
Mick
 
Upvote 0
not from sheet nor hardcode.
im getting from a textbox.

thanks for your help.
i have sorted out and now it works fine
Code:
Private Sub Add_Click()
ListBox1.AddItem TextBox2.Value
TextBox2.Value = ""
End Sub
Private Sub Remove_Click()
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub

hope this helps someone else
 
Last edited:
Upvote 0
This lets you add more than one item if you set TextBox1.Multipline = True.
Code:
Private Sub CommandButton1_Click()
  Dim a, v
  a = Split(TextBox1.Value, vbCrLf)
  With ListBox1
    For Each v In a
      .AddItem v, .ListCount
    Next v
  End With
  TextBox1.Value = vbNullString
End Sub

Private Sub CommandButton2_Click()
  With ListBox1
    If .ListIndex < 0 Then Exit Sub
    .RemoveItem .ListIndex
  End With
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  CommandButton2_Click
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top