listbox delete row code stopped functioning

dss28

Board Regular
Joined
Sep 3, 2020
Messages
137
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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
You have a typo here
Rich (BB code):
Selected_List1 = i + 1
 
Solution

dss28

Board Regular
Joined
Sep 3, 2020
Messages
137
Office Version
  1. 2007
Platform
  1. Windows
oh god... such a stupid mistake
deleted 1 from Selected_List1 ----- now working fine, tested with and without 1

thanks a lot Fluff
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.

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

dss28

Board Regular
Joined
Sep 3, 2020
Messages
137
Office Version
  1. 2007
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
My pleasure.
 

Forum statistics

Threads
1,148,257
Messages
5,745,709
Members
423,968
Latest member
CHHeights

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
Top