Employee training database: Delete an employee completely

Ninja_nutter

New Member
Joined
Mar 1, 2016
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

At work I have inherited an Excel employee training database. The details of an employee are entered into a user form and can be edited/updated or deleted. The issue I have is that when you go to delete an employee completely, you have to select the employee from the listbox of training courses then delete each line entry. As the criteria is looking for the employees unique employment number, I would like the code to loop through until that unique identifier is completely deleted.

Here is the code:

Code:
Private Sub cmdDeleteA_Click()
    'declare the variables
    Dim findvalue As Range
    Dim cDelete As VbMsgBoxResult
    Dim cNum As Integer


'error statement
        On Error GoTo errHandler:
'check for values
        If Reg1.Value = "" Or Reg4.Value = "" Then
             MsgBox "There is not data to delete"
        Exit Sub
    End If


    'give the user a chance to change their mind
         cDelete = MsgBox("Are you sure that you want to delete this person", vbYesNo + vbDefaultButton2, "Are you sure????")
    If cDelete = vbYes Then


    'find the employee
        Set findvalue = Sheet2.Range("F:F").Find(What:=Reg4, LookIn:=xlValues)
        findvalue.EntireRow.Delete
    End If


    'clear the controls
        cNum = 9
        For X = 1 To cNum
        Me.Controls("Reg" & X).Value = ""
    Next


'run the filter
    AdvFilter


'add the values to the listbox
    lstLookup.RowSource = ""
    lstLookup.RowSource = "Filter_Staff"
'error block
    On Error GoTo 0
    Exit Sub


errHandler::
    MsgBox "An Error has Occurred  " & vbCrLf & "The error number is:  " _
           & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Please notify the administrator"
End Sub

As i am a newbie to VBA coding any help would be greatly appreciated.

Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,
untested but see if this update to your code does what you want


Code:
Private Sub cmdDeleteA_Click()
'declare the variables
    Dim findvalue As Range, deleterange As Range
    Dim cDelete As VbMsgBoxResult
    Dim cNum As Integer
    Dim firstaddress As String, Search As String
    
    
'error statement
    On Error GoTo errHandler:
    
    Search = Reg4.Value
    
'check for values
        If Len(Search) = 0 Then MsgBox "There is not data to delete", 48, "No Data": Reg4.SetFocus: Exit Sub
    
'find the employee
    Set findvalue = Sheet2.Range("F:F").Find(What:=Search, LookIn:=xlValues, lookat:=xlWhole)
    
    If Not findvalue Is Nothing Then
        
'give the user a chance to change their mind
        cDelete = MsgBox(Search & Chr(10) & _
                        "Are you sure that you want to delete this person.", 292, "Are you sure????")
        If cDelete = vbNo Then Exit Sub
        
'mark first match address
        firstaddress = findvalue.Address
'create union of matching ranges
        Do
            If deleterange Is Nothing Then
                Set deleterange = findvalue
            Else
                Set deleterange = Union(deleterange, findvalue)
            End If
'find next match
            Set findvalue = Sheet2.Range("F:F").FindNext(findvalue)
            
        Loop While firstaddress <> findvalue.Address
        
'delete all matches in one go
        If Not deleterange Is Nothing Then deleterange.EntireRow.Delete
        
'clear the controls
        cNum = 9
        For X = 1 To cNum
            Me.Controls("Reg" & X).Value = ""
        Next
        
'run the filter
        AdvFilter
           
'add the values to the listbox
        lstLookup.RowSource = ""
        lstLookup.RowSource = "Filter_Staff"
        
        MsgBox Search & Chr(10) & "Record(s) Deleted", 64, "Record Deleted"
        
    Else
    
        MsgBox Search & Chr(10) & "Record Not Found", 48, "Not Found"
        
    End If
    
errHandler:
    If Err <> 0 Then
        MsgBox "An Error has Occurred  " & vbCrLf & "The error number is:  " _
        & Err.Number & vbCrLf & Err.Description & vbCrLf & _
        "Please notify the administrator", 48, "Error"
    End If
End Sub


As a thought, rather than permanently deleting records, consider archiving them to another sheet or workbook for future reference.

Hope Helpful

Dave
 
Upvote 0
Hi Dave,

That works really well!! Thank you so much.

We have considered archiving the employee's records, however the senior management decided against it.
 
Upvote 0
Hi Dave,

That works really well!! Thank you so much.

We have considered archiving the employee's records, however the senior management decided against it.

Glad update did what you wanted

If want to archive, would quite simple to modify code in future.

Forgot to add in my post that I removed this part in your code

Code:
If Reg1.Value = ""

as it was not doing anything - I wondered if it may be used as Forename maybe? but control naming convention not clear.


Dave
 
Upvote 0
Glad update did what you wanted

If want to archive, would quite simple to modify code in future.

Forgot to add in my post that I removed this part in your code

Code:
If Reg1.Value = ""

as it was not doing anything - I wondered if it may be used as Forename maybe? but control naming convention not clear.


Dave

Hi Dave,

Noted. Reg1 value was a surname, however Reg4 is the unique employee number.

Thanks again for the help. :)
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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