Vba listbox delete selected row
Ngày đăng:
04/01/2022
Trả lời:
0
Lượt xem:
13
Hi guys, newbie here. I've been working on this project for weeks, and cant seem to make this one feature work. I have a ListBox called ListBox1 that uses RowSource for the data. Now what I would really like is to be able to delete a row from the worksheet depending on the item selected in the ListBox. I have used the delete last row method, but because the ListBox contains alot of data, I would prefer to have ability to select the item I wanted to delete. Any help? Much Appreciated, Nick Try this on some test data... Sub CommandButton3_Click() ' Will move data to array and then move wanted data ' to a second array and repaste that back on to the spreadsheet ' currently TestCondition is the comparison in column A ' Change Sheet1 if your worksheet name is different. Const COMPARE_COL As Long = 1 Dim a, aNew(), nr As Long, nc As Long Dim r As Long, c As Long, rNew As Long Dim TestCondition As String, tmp a = Worksheets("Sheet1").UsedRange nr = UBound(a, 1) nc = UBound(a, 2) ReDim aNew(1 To nr, 1 To nc) rNew = 0 TestCondition = lstDisplay.Selected(a) For r = 1 To nr tmp = a(r, COMPARE_COL) If tmp <> TestCondition Then rNew = rNew + 1 For c = 1 To nc aNew(rNew, c) = a(r, c) Next c TestCondition = tmp End If Next r Worksheets("Sheet1").UsedRange = aNew End Sub
|