rorobear
02-10-2023, 11:19 AM
Hello Everyone,
I can not seem to get my If and Elseif statement to work correctly, and could really use some help. Here is what I’m trying to do:
If no selection is made from the down, a message appears:
MsgBox "Select a Worksheet From the Dropdown", vbInformation, " Worksheet Selection"
so User selects a worksheet
If no selection is made from the Listbox another message appears:
MsgBox "Now Make a Selection from the Listbox", vbInformation, "ListBox Selection"
so User selects form ListBox
And finally, once a selection is made from the ListBox, the code runs as intended.
The workbook is attached.
Private Sub DeleteSelection_Click()
Dim lngListBoxIndex As Long
Dim strRecName As String
If Me.ComboBox1.Value = "" Then
MsgBox "Select a Worksheet From the Dropdown", vbInformation, "Select Worksheet"
Exit Sub
End If
If Frm_ViewData.ListBox1.Selected(0) = True Then
MsgBox "Now Make a Selection from the Listbox."
Else: End If
With Me.ListBox1
For lngListBoxIndex = 0 To .ListCount - 1
If .Selected(lngListBoxIndex) = True Then
strRecName = .List(.ListIndex, 0)
If MsgBox("Are you sure you want to delete the record?", vbQuestion + vbYesNo, "Delete Record") = vbYes Then
Application.ScreenUpdating = False
'--- >modified code in order for it to delete based on item selected in the ComboBox1
Sheets(ComboBox1.Value).ListObjects(1).DataBodyRange(lngListBoxIndex + 1, 1).EntireRow.Delete
Application.ScreenUpdating = True
End If
Exit For
End If
Next lngListBoxIndex
End With
Call ComboBox1_change '---> update ListBox after the row is deleted
MsgBox "Record has Been Deleted", vbInformation, "Delete Record"
End Sub
I can not seem to get my If and Elseif statement to work correctly, and could really use some help. Here is what I’m trying to do:
If no selection is made from the down, a message appears:
MsgBox "Select a Worksheet From the Dropdown", vbInformation, " Worksheet Selection"
so User selects a worksheet
If no selection is made from the Listbox another message appears:
MsgBox "Now Make a Selection from the Listbox", vbInformation, "ListBox Selection"
so User selects form ListBox
And finally, once a selection is made from the ListBox, the code runs as intended.
The workbook is attached.
Private Sub DeleteSelection_Click()
Dim lngListBoxIndex As Long
Dim strRecName As String
If Me.ComboBox1.Value = "" Then
MsgBox "Select a Worksheet From the Dropdown", vbInformation, "Select Worksheet"
Exit Sub
End If
If Frm_ViewData.ListBox1.Selected(0) = True Then
MsgBox "Now Make a Selection from the Listbox."
Else: End If
With Me.ListBox1
For lngListBoxIndex = 0 To .ListCount - 1
If .Selected(lngListBoxIndex) = True Then
strRecName = .List(.ListIndex, 0)
If MsgBox("Are you sure you want to delete the record?", vbQuestion + vbYesNo, "Delete Record") = vbYes Then
Application.ScreenUpdating = False
'--- >modified code in order for it to delete based on item selected in the ComboBox1
Sheets(ComboBox1.Value).ListObjects(1).DataBodyRange(lngListBoxIndex + 1, 1).EntireRow.Delete
Application.ScreenUpdating = True
End If
Exit For
End If
Next lngListBoxIndex
End With
Call ComboBox1_change '---> update ListBox after the row is deleted
MsgBox "Record has Been Deleted", vbInformation, "Delete Record"
End Sub