Deleting listbox list, from listbox and source

blaker

New Member
Joined
Jul 1, 2013
Messages
29
Office Version
  1. 365
Platform
  1. Windows
I would like to double click on one of the items / rows of data in the list box and delete that item in the list box as well as the source in the spreadsheet. this is multi column, the sheet is Market and the Range is "AA7:aq26" when the listbox data is loaded. I have the below code but it only deletes the row from the list box not the source?

Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Long
Dim OriginalCount As Long

'Store original ListBox count
OriginalCount = ListBox3.ListCount



'Delete selected line items
For x = OriginalCount - 1 To 0 Step -1
If ListBox3.Selected(x) = True Then ListBox3.RemoveItem x
Next x

see below the form open in front of the spreadsheet sorcedata for the listbox.
Thanks!
Blake

1622669498281.png
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,233
Office Version
  1. 2007
Platform
  1. Windows
Okay, let's try to delete the record using column AK as a key.

Update your ListBox3_DblClick code with the following:

VBA Code:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim x As Long
  Dim OriginalCount As Long
  Dim f As Range
  Dim sh As Worksheet
  Dim customer As String
  
  Set sh = Sheets("Market")
  'Delete selected line items
  For x = ListBox3.ListCount - 1 To 0 Step -1
    If ListBox3.Selected(x) = True Then
      customer = ListBox3.List(x, 10)
      If customer <> "" Then
        Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          f.EntireRow.Delete
          ListBox3.RemoveItem x
        End If
      End If
    End If
  Next x
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

blaker

New Member
Joined
Jul 1, 2013
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hey this works perfectly! however I have formulas in the adjacent cells that calculate cost and don't really want to delete the entire row, would there be a way to in the selected row delete the values in "Y:AD", "AF:AM" and "AO" and the shift cells up...
is there a way to do this? never the less I really appreciate the code you have provided so far!!!
Thanks!
Blake
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,233
Office Version
  1. 2007
Platform
  1. Windows
delete the values in "Y:AD", "AF:AM" and "AO" and the shift cells up
Try this:

VBA Code:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim x As Long
  Dim OriginalCount As Long
  Dim f As Range
  Dim sh As Worksheet
  Dim customer As String
  
  Set sh = Sheets("Market")
  'Delete selected line items
  For x = ListBox3.ListCount - 1 To 0 Step -1
    If ListBox3.Selected(x) = True Then
      customer = ListBox3.List(x, 10)
      If customer <> "" Then
        Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          sh.Range("Y" & f.Row & ":AD" & f.Row & _
                   ", AF" & f.Row & ":AM" & f.Row & _
                   ", AO" & f.Row).Delete shift:=xlUp
          ListBox3.RemoveItem x
        End If
      End If
    End If
  Next x
End Sub
 
Solution

blaker

New Member
Joined
Jul 1, 2013
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Dim x As Long Dim OriginalCount As Long Dim f As Range Dim sh As Worksheet Dim customer As String Set sh = Sheets("Market") 'Delete selected line items For x = ListBox3.ListCount - 1 To 0 Step -1 If ListBox3.Selected(x) = True Then customer = ListBox3.List(x, 10) If customer <> "" Then Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False) If Not f Is Nothing Then sh.Range("Y" & f.Row & ":AD" & f.Row & _ ", AF" & f.Row & ":AM" & f.Row & _ ", AO" & f.Row).Delete shift:=xlUp ListBox3.RemoveItem x End If End If End If Next x
This works perfect!!!

Thanks so much!!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,233
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,136,215
Messages
5,674,435
Members
419,510
Latest member
greatglern

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