I can't delete row from sheet by selected row in listbox on userform except last row

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
H ello
I try deleting from sheet by selected row in listbox except the last row based on selected sheet from combobox , but doesn't happens anything !
here is the code
VBA Code:
Private Sub CommandButton2_Click()
With Me.ListBox1
        If .ListIndex = .ListCount - 1 Then
            MsgBox "you have not permission to do that", vbExclamation
            Exit Sub
        End If
    End With
  Dim i As Integer
  Application.ScreenUpdating = False
  Set ws = Sheets(ComboBox1.Value)
  If MsgBox("Are you sure you want to delete this data row?", vbYesNo + vbQuestion, "Delete Row?") = vbYes Then
   With ws
    For i = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
      If .Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then
        .Rows(i).Delete
      End If
    Next i
    End With
  End If
  Application.ScreenUpdating = True
End Sub
I hope somebody has idea .
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
if you mean I should check variable i will gives me the values by looping through rows 1,2,...
if the i is zero then could be problem .
 
Upvote 0
I meant what about the code example that was given there?
Easy enough to check what i is
For i = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
debug.print i
rest of code here
 
Upvote 0
sorry for delaying
VBA Code:
I meant what about the code example that was given there?
not really understand it!
do you mean what gives in original project?
 
Upvote 0
yes , but DATE not value
Try replacing this:
VBA Code:
      If .Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then
with this:
VBA Code:
      If .Cells(i, 1) = CDate(ListBox1.List(ListBox1.ListIndex)) Then
 
Upvote 0
Hi Akuini,
I just want deleting row selection ,but your suggestion will delete all of rows contains duplicated date .
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

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