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,330
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,339
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,330
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,330
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Would it be better to have the names just in the list box or on the separate sheet?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,339
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows

ADVERTISEMENT

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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,339
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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 ??
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,330
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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.
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,132,640
Messages
5,654,537
Members
418,138
Latest member
agnesegras

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
Top