:-( dont know how to do this VBA

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

i have a userform listbox which polulates data from a my worksheet

now here is what i want to do on the userform

i want to have a delete and edit command buttons

from the listbox..if i select a record and press delete then i want to look in my sheet and delete that record and edit the record on the sheet based on what amendments i make on the userform (i have text boxes to put my amendments in) but the main thing is i cant figure out how to delete or modify based on record selected in the userform
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It depends on how you are populating the listbox and what range you are populating it from.

It could be very simple, for example if you've populated from a contiguous range then the ListIndex of the selecte item will correspond to the row it comes from in the range.

So with that you could do the delete/amend on the appropriate row.
 
Upvote 0
Hi Norie,

can you please show me how this could be done. I am copying from a contiguos range from the worksheet

thank you
 
Upvote 0
How can you populate a listbox by copying from a range?
 
Upvote 0
Hi Norie

i did it like this

listbox.rowsource=sheets("sheet1").range("a1:c100").address(0,0)
 
Upvote 0
Replace that with this.

Code:
listbox.List=Sheets("sheet1").Range("A1:C100").Value

You can then use this to delete the selected item.
Code:
Private Sub cmdDelete_Click()
Dim idx As Long

       idx = listbox.ListIndex


       If idx = -1 Then
           MsgBox "No item selected."
       Else
           Sheets("Sheet1").Range("A" & idx+1 & ":C" & idx+1).Delete Shift:=xlShiftUp
       End If

End Sub

To update the values for the selected item.
Code:
Private Sub cmdUpdate_Click()
Dim rng As Range
Dim idx As Long

       idx = listbox.ListIndex

       If idx = -1 Then
           MsgBox "No item selected."
       Else
           Set rng = Sheets("Sheet1").Range("A" & idx + 1)

           rng.Value = Textbox1.Value
           rng.Offset(,1) = Textbox2.Value
           rng.Offset(,2) = Textbox3.Value
       End If

End Sub

You'll probably need to adjust the code to suit your own setup, but that's the general idea of how to delete and amend based on a listbox.

PS If you have a header in row 1 then you'll need to use idx + 2 instead of idx + 1.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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