I'm making a User form with a listbox and a delete button. The list box grab data from a table and display it into the listbox row. When clicking on Delete, I added in a function to tell the user to select a row if no row from the listbox has been selected. I tried to added in a function to tell the user there's nothing to delete if they select a empty row. It didn't work as intended and just continued on with the script.
Here's what the start of the script looks like, let me know if I need to add in the rest of the script.
Private Sub Delete_Click()
If Not ListBox1.Selected(ListBox1.ListIndex) Then
MsgBox "Please select a row to delete"
Exit Sub
End If
If Len(Trim(ListBox1.List(ListBox1.ListIndex))) = 0 Then '<-- this is the line that dont work
MsgBox "Nothing to delete here"
Exit Sub
End If
Dim response As VbMsgBoxResult
response = MsgBox("Are you sure you want to delete the selected rows?", vbYesNo, "You sure?")
'If no then do nothing
If response = vbNo Then
Exit Sub
Else
Dim selectedRows As New Collection
Dim i As Integer
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Table")
'Store the indices of the selected rows in a collection
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
selectedRows.Add i
End If
Next i
'Delete the rows from the ListBox and the data source
If selectedRows.Count > 0 Then
'Sort the indices in descending order to avoid issues with deleting rows while iterating through the collection
For i = selectedRows.Count To 1 Step -1
ListBox1.RemoveItem selectedRows(i)
Worksheets("Table").ListObjects("Table1").ListRows(selectedRows(i) + 1).Delete
Next i
End If
Dim lastRow As Long
lastRow = Worksheets("Table").Range("Q" & Rows.Count).End(xlUp).row 'find last filled cell in column Q
Dim n As Long
For n = 4 To lastRow ' start with Q4
If Not IsEmpty(Worksheets("Table").Range("Q" & n)) Then
Worksheets("Table").Range("Q" & n).Value = n - 3 'subtract 3 to start at 1 instead of 4
End If
Next n
ListBox1.Clear
Call UserForm_Initialize
End If
End Sub
I've also tried these but no luck
If ListBox1.ListIndex > -1 And Len(ListBox1.List(ListBox1.ListIndex)) = 0 Then
MsgBox "Nothing to delete here"
Exit Sub
End If
If ListBox1.ListIndex > -1 And ListBox1.ListCount > 0 And Trim(ListBox1.List(ListBox1.ListIndex)) = "" Then
MsgBox "Nothing to delete here"
Exit Sub
End If
If Not ListBox1.Selected(ListBox1.ListIndex) Then
MsgBox "Please select a row to delete"
Exit Sub
ElseIf Len(ListBox1.List(ListBox1.ListIndex)) = 0 Then
MsgBox "Nothing to delete here"
Exit Sub
End If
Solved, Idk why it's treating a empty row as a special character. Using replace, we turn that special character into "". Idk how it works, but it does. If someone can shine a light on what is going on, please do so.
ElseIf Len(Replace(ListBox1.List(ListBox1.ListIndex), vbTab, "")) = 0 Then
MsgBox "Nothing to delete here"
Exit Sub
End If
Okay so I started throwing spaghettis at the wall and it finally stuck. So I believe this replace tab characters with "". IDK why there are tab characters in there in the first place when my script to fill the table dont put any tab characters in.
ElseIf Len(Replace(ListBox1.List(ListBox1.ListIndex), vbTab, "")) = 0 Then
MsgBox "Nothing to delete here"
Exit Sub
End If