Problem with delete button code

RAires

New Member
Joined
Aug 31, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hello guys.

I'm making a form and i added a delete button to a database that i already had.

I created a list box inside the form so you can see the database while you're filling the form. The idea of my delete button is to be able to select a row in the listbox and be able to press delete and delete that row.
The problem i'm running into is that when i add data to the form and then go to the last entry i added and try to delete it, it deletes another row instead i cant i figure out what is wrong with my code.

If you guys could help would be awesome.

Here is the code i got for the delete button:
VBA Code:
Private Sub cmdDelete_Click()
Dim i As Integer

For i = 0 To Range("B65356").End(xlUp).Row - 1
    If lstDisplay.Selected(i) Then
        Rows(i + 1).Select
        Selection.Delete
    End If
Next i
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
variable i is for the rows on your sheet...
there is no row 0 and Range("B65356").End(xlUp).Row - 1 will be the row before the last row.
 
Upvote 0
Any tips on how to fix it? I've been changing values arround but i dont seem to be able to make it work. I'm sorry it's probably a simple solution but im new to this.

Thanks for the reply i really appreciate it
 
Upvote 0
List boxes use 0 based arrays.

It almost lookes like

Range("A1").Offset(lstDisplay.ListIndex, 0).EntireRow is the row you want deleted.

Is this a single select or multi-select list box?
 
Upvote 0

Forum statistics

Threads
1,215,250
Messages
6,123,887
Members
449,131
Latest member
leobueno

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