Deleting a name from a list by selecting from a list box and pressing a button

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a worksheet that has a dynamic list of names in column A on the sheet with the code name YP. The list starts in A3 and can have names added to it. I have a listbox called lboTracker on the sheet with the code name Tracker as is populated with the list of names. I want to be able to click on a name in the list box then select a button to delete it from the list. Could someone show me how would I do this please?

I have this code so far
VBA Code:
Option Explicit
Sub OpenFile()
    Dim SelectedItem As String
    SelectedItem = Cells(1, 1).Value
    Workbooks.Open Filename:=ThisWorkbook.Path & "\Young People\" & SelectedItem & ".xlsm"
End Sub

Sub AddYP()
Dim NewYP As String

NewYP = Tracker.Cells(5, 4)
YP.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = NewYP
    
End Sub

Sub PopLB()
    Dim Lastrow As Long
    Lastrow = YP.Cells(Rows.Count, "A").End(xlUp).Row
    Tracker.lboYP.List = YP.Range("A2:A" & Lastrow).Value
End Sub

Sub Sort()
Range("A2", Range("A2").End(xlDown)).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Is this what your after
VBA Code:
Sub MM1()
 For i = ListBox1.ListCount - 1 To 0 Step -1'change listbox name to suit
    If ListBox1.Selected(i) Then
        ListBox1.RemoveItem i
    End If
Next i
End Sub
 
Upvote 0
Thanks for the reply Michael. I ran this code and it deleted all the data in the list box and it did not remove the item from the list on the sheet YP.

VBA Code:
Sub RemoveName()
Dim Name As String, i As Long
    For i = Tracker.lboYP.ListCount - 1 To 0 Step -1 'change listbox name to suit
        If Tracker.lboYP.Selected(i) Then
            Tracker.lboYP.RemoveItem i
        End If
    Next i
End Sub
 
Upvote 0
Would it be better to have the names just in the list box or on the separate sheet?
 
Upvote 0
Don't have Excel ATM, so can't really try it.
You will need to do a FIND ? Replace code to remove the item from the list on the sheet....and would be delete the entire row I'm guessing
 
Upvote 0
Why not simply delete the required name and then change the list range in the listBox Properties. I mean, how often will a name be deleted ??
 
Upvote 0
I guess that is a good point Michael, I am just trying to make more work for myself. Maybe it is good as it is without that feature.

Thanks anyway.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

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