Delete matching rows in table not deleting all

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have written this code on my userform to allow the user to select the employee from CBO_EMPLOYEE, then push the command button and the records for that employee will be deleted. I will take care of warnings and errors after I get this code to work as it is intended.

Problem 1: Example: Employee has 7 records, the code only deletes 4, leaving 3 in the table. On the same employee I run the code again and it deletes 2 of the 3. The employee name to match to the combobox is in column 3 of Table1. Unfortunately, I cannot change the layout of the table. It should not matter what information is any other columns except 3.

Problem 2: I would rather not unhide the Absentee worksheet when the userform is ran, how could I leave it hidden and change the ActiveSheet code to work without unhiding the worksheet with the table of employees?

VBA Code:
Private Sub UserForm_Initialize()

Application.ScreenUpdating = False
Worksheets("Absentee").Visible = True
Worksheets("Absentee").Activate

'######################################################## POPULATE UNIQUE EMPLOYEE LIST
Set ws = Worksheets("Absentee")
Dim v, e
With Sheets("Absentee").Range("Table1[Employee]")
v = .value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.CBO_EMPLOYEE.List = Application.Transpose(.keys)
End With
    Application.ScreenUpdating = True
End Sub
'====================================================================================
Private Sub CMD_DeleteRow_Click()
Application.ScreenUpdating = False
Worksheets("Absentee").Visible = True
    Worksheets("Absentee").Activate
Dim xterm As Range
Dim LastRow As Long

For Each xterm In ActiveSheet.ListObjects(1).ListColumns(3).DataBodyRange.Cells
If xterm.value = CBO_EMPLOYEE.value Then
xterm.EntireRow.Delete Shift:=xlUp
End If
    Next
    Worksheets("report").Activate
Worksheets("Absentee").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
When you delete rows you should start from the bottom of the sheet and work upwards. I suspect that is causing the problem. You could use a different approach by filtering the table based on the employee name in column 3 and deleting the visible rows not including row 1 if it has headers then remove the filter. This avoids looping which can be slow and eliminates your current problem.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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