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,507
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Keep the entire process as simple as possible. This macro allows the user to manually select the row, click the button and the row has been deleted.

VBA Code:
Option Explicit

Sub gobyby()

On Error Resume Next
Dim ws1 As Worksheet
Set ws1 = Worksheets(1)
    If Not ws1 Is Nothing Then
        With ws1
            ActiveCell.EntireRow.Delete
        End With
    End If
End Sub
 
Upvote 0
When someone says "nothing happens" and we read things like If .Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then, we have no idea if the condition is true or not. Step through your code (F8) and check the values of references and variables as you go. This is troubleshooting 101. If
If .Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then
is not true then you can watch and see that the delete line does not highlight, so it is not True.
 
Upvote 0
@Logit
I suppose your code does that from inside the sheet, but I need from listbox on userform because I enter data on userform and copy to sheet , sometimes enter wrong data on userform so I need delete from userform without enter inside sheet and delete it .
 
Upvote 0
@Micron
but the code works without any problem with another project, and now suddenly doesn't work !
 
Upvote 0
You can paste the code to a CommandButton1 that is attached to UserForm1 :

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Set ws1 = Worksheets(1)
    If Not ws1 Is Nothing Then
        With ws1
            ActiveCell.EntireRow.Delete
        End With
    End If
End Sub
 
Upvote 0
thanks logit I still want by selected row from listbox on userform .
 
Upvote 0
the code works without any problem with another project,
So what? Obviously something is different with this project and you need to troubleshoot it as I suggested, but not only for the example I used. All references and variables must be as you expect.
 
Upvote 0
So what? Obviously something is different with this project
I don't think big difference but I will do somethings to find out if you're right .
thanks
 
Upvote 0
I'm not really sure why !
the original project will be as in new project with the same structure !:confused:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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