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?
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