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 .
 
Hi Akuini,
I just want deleting row selection ,but your suggestion will delete all of rows contains duplicated date .
Could you post a sample data & explain what you're trying to do in more detail?
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
CRT.xlsm
ABCDE
1DATEIDINPUTOUTPUTBALANCE
22022/11/21IDD-CLOTT1003,420.00-3,420.00
32022/11/21IDD-CLOTT1013,350.00-6,770.00
42022/11/21IDD-CLOTT1025,270.00-1,500.00
52022/11/21IDD-CLOTT1031,500.000.00
62022/11/22IDD-CLOTT104840.00-840.00
72022/11/22IDD-CLOTT1052,550.00-3,390.00
82022/11/23IDD-CLOTT1063,390.000.00
92022/11/24IDD-CLOTT107980.00-980.00
102022/11/25IDD-CLOTT108300.00-1,280.00
112022/11/26IDD-CLOTT109300.00-1,580.00
122022/11/27IDD-CLOTT1101,580.000.00
13TOT11,740.0011,740.000.00
CMT
Cell Formulas
RangeFormula
C13:D13C13=SUM(C2:C12)
E13E13=C13-D13


before
1.PNG





after

CRT.xlsm
ABCDE
1DATEIDINPUTOUTPUTBALANCE
22022/11/21IDD-CLOTT1003,420.00-3,420.00
32022/11/21IDD-CLOTT1013,350.00-6,770.00
42022/11/21IDD-CLOTT1025,270.00-1,500.00
52022/11/22IDD-CLOTT104840.00-840.00
62022/11/22IDD-CLOTT1052,550.00-3,390.00
72022/11/23IDD-CLOTT1063,390.000.00
82022/11/24IDD-CLOTT107980.00-980.00
92022/11/25IDD-CLOTT108300.00-1,280.00
102022/11/26IDD-CLOTT109300.00-1,580.00
112022/11/27IDD-CLOTT1101,580.000.00
12TOT10,240.0011,740.00-1,500.00
CMT
Cell Formulas
RangeFormula
C12:D12C12=SUM(C2:C11)
E12E12=C12-D12

and should not delete the last row if the user try deleting as the original code does it.
 
Upvote 0
1. How did you populate the listbox? using .Rowsource or .List?
2. Do you want to delete the row only in the sheet or also in the listbox?
 
Upvote 0
1. How did you populate the listbox? using .Rowsource or .List?
by select sheet from com combobox1
I use this code
VBA Code:
Private Sub UserForm_Initialize()
 
 Crit = ""
  Dim i As Long
  For i = 1 To Sheets.Count
   If Sheets(i).Name <> "MAIN" And Sheets(i).Name <> "DATA" Then
   ComboBox1.AddItem Sheets(i).Name
   End If
  Next
  
  If ComboBox1.ListIndex > -1 Then
    Set ws = Sheets(ComboBox1.Value)
    Call LBoxPop
    
  End If
also in the listbox?
yes for two both
 
Upvote 0
I assume that data in col B is unique. So you can use that to identify the correct row to delete. Maybe like this:
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

  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
        Set c = ws.Range("B:B").Find(What:=ListBox1.List(ListBox1.ListIndex, 1), LookIn:=xlValues, lookAt:=xlWhole, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not c Is Nothing Then
            c.EntireRow.Delete
            ListBox1.RemoveItem ListBox1.ListIndex
        Else
            MsgBox "Item not found"
        End If
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
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