listbox delete row code stopped functioning

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
I have a userform in which I have refresh button to load the listbox with data from a worksheet.

A delete button to select a row for deletion etc.

The delete code was functioning for several months before but suddenly is stopping now at the "no row is selected" stage even after selecting the row in the listbox.

Need advice why it might be stopping now.

The code for row selection and delete command is below:


VBA Code:
[CODE=vba]Private Sub CommandButton5_Click()         'delete row

        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        Application.DisplayStatusBar = False
        Application.EnableEvents = False
        ActiveSheet.DisplayPageBreaks = False

    If Selected_List = 0 Then

        MsgBox "No row is selected.", vbOKOnly + vbInformation, "Delete"
        Exit Sub

    End If

    Dim i As VbMsgBoxResult

    i = MsgBox("Do you want to delete the selected record?", vbYesNo + vbQuestion, "Delete")

    If i = vbNo Then Exit Sub

    Dim row As Long

    row = Me.ListBox1.List(Me.ListBox1.ListIndex, 0) + 1

    ThisWorkbook.Sheets("Data").Rows(row).Delete

     MsgBox "Selected records has been deleted successfully.", vbOKOnly + vbInformation, "Delete"


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True

 
End Sub


Function Selected_List() As Long

    Dim i As Long

    Selected_List = 0

    If UserForm8.ListBox1.ListCount < 1 Then Exit Function

    For i = 0 To UserForm8.ListBox1.ListCount - 1

        If UserForm8.ListBox1.Selected(i) = True Then

            Selected_List1 = i + 1

            Exit For

        End If

    Next i

End Function
[/CODE]
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You have a typo here
Rich (BB code):
Selected_List1 = i + 1
 
Upvote 0
Solution
oh god... such a stupid mistake
deleted 1 from Selected_List1 ----- now working fine, tested with and without 1

thanks a lot Fluff
 
Upvote 0
You're welcome & thanks for the feedback.

If were using Option Explicit, it would have picked up the problem.
 
Upvote 0
You're welcome & thanks for the feedback.

If were using Option Explicit, it would have picked up the problem.

I try to avoid option explicit as I am not so much aware of the full programming and declaration part, but will keep this in mind when stuck with such issues.
Thanks a lot again
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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