Select row in listbox and find matching entry

learningthings

New Member
Joined
Oct 29, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I have a listbox containing a list of transactions that happened in one instance, for example a sale and a refund.
Each transaction has an ID attached to it and is recorded on its own respective sheets. I want to create a button that can match the ID from the selected listbox entry to an entry on either the sales or refund worksheet and delete the entry.
Any ideas on how I could do this?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I want to create a button that can match the ID from the selected listbox entry to an entry on either the sales or refund worksheet and delete the entry.

Could you answer the following questions:
  • You can put here the instruction you use to load the data in the listbox.
  • Is the ID unique?
  • It must be in one of the 2 sheets?
  • What are the two sheets called?
  • In which column do you store the ID, is it the same column in the 2 sheets?
 
Upvote 0
Could you answer the following questions:
  • You can put here the instruction you use to load the data in the listbox.
  • Is the ID unique?
  • It must be in one of the 2 sheets?
  • What are the two sheets called?
  • In which column do you store the ID, is it the same column in the 2 sheets?
This is the code I use to load the listbox, it is named lstRegister and loads the sheets "Register View" which just displays the transactions on that page.
VBA Code:
With frmRegister
Dim ws As Worksheet
Dim rng As Range
Dim MyArray
Set ws = Sheets("RegisterView")
Set rng = ws.Range("A1:I" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)

With .lstRegister
       .Clear
       .ColumnHeads = False
       .ColumnCount = rng.Columns.Count
        '~~> create a one based 2-dim datafield array
        MyArray = rng
         '~~> fill listbox with array values
         .List = MyArray
         '~~> Set the widths of the column here. Ex: For 5 Columns
         '~~> Change as Applicable
         .ColumnWidths = "70;70;70;70;70;70;70;70;70"
         .TopIndex = 0
         .ListIndex = .ListCount - 1
    End With

End With
Yes, the ID is unique to each transaction and must be located in one of the two sheets. The ID is stored in column A for both sheets. The two sheets are named "Sales" and "Refunds".
Thank you for your help!
 
Upvote 0
Try this.
Select the ID and press a button.
In a commandbutton put the following code:

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Dim sh As Worksheet
  Dim shs As Variant
  Dim i As Long
  
  shs = Array("Sales", "Refunds")
  
  With lstRegister
    If .ListIndex = -1 Then
      MsgBox "Select a item"
      Exit Sub
    End If
    
    For i = 0 To UBound(shs)
      Set sh = Sheets(shs(i))
      Set f = sh.Range("A:A").Find(.List(.ListIndex, 0), , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        If MsgBox("ID on sheet: " & sh.Name & vbCr & _
                  "Do you want to delete", vbQuestion + vbYesNo) = vbYes Then
          f.EntireRow.Delete
          Exit For
        Else
          Exit For
        End If
      End If
    Next
    
    If f Is Nothing Then
      MsgBox "ID not found"
    End If
  End With
End Sub
 
Upvote 0
Solution
Try this.
Select the ID and press a button.
In a commandbutton put the following code:

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Dim sh As Worksheet
  Dim shs As Variant
  Dim i As Long
 
  shs = Array("Sales", "Refunds")
 
  With lstRegister
    If .ListIndex = -1 Then
      MsgBox "Select a item"
      Exit Sub
    End If
   
    For i = 0 To UBound(shs)
      Set sh = Sheets(shs(i))
      Set f = sh.Range("A:A").Find(.List(.ListIndex, 0), , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        If MsgBox("ID on sheet: " & sh.Name & vbCr & _
                  "Do you want to delete", vbQuestion + vbYesNo) = vbYes Then
          f.EntireRow.Delete
          Exit For
        Else
          Exit For
        End If
      End If
    Next
   
    If f Is Nothing Then
      MsgBox "ID not found"
    End If
  End With
End Sub
Thank you so much! It worked wonderfully
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
Hi Dante! I had another quick question if you wouldn't mind.
I am now coding an edit button. The way each sale is recorded is through a form that details item, quantity, subtotal, etc.
I would like to be able to code the edit button to search the worksheets "Sales" and "Refunds" for the matching entry ID (similar to the delete button) but instead, pull up the form and have the relevant data prefilled. Then, if I hit save, it saves this edit.
What do you think?
 
Upvote 0
It's a different topic and a different macro. You could create a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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