Dear Excel users, I have created a userform with textboxes that Adds a staff members first name, last name and address on sheet 3 called "Lists". It is working perfectly - How can I also search and delete the same records. Any Suggestions?? All help will be greatly appreciated!!
Code:
Private Sub btn_Add_Click()
Dim X As Integer
Dim nextrow As Range
Dim cNum As Integer
Dim ws As Worksheet
Dim ans As Long
On Error GoTo errHandler:
'find the next row
Set nextrow = Sheet3.Cells(rows.Count, 3).End(xlUp).Offset(1, 0)
Application.ScreenUpdating = False
'End If
If Me.Reg1.Value = "" Or Me.Reg2.Value = "" Or Me.Reg3.Value = "" Then
MsgBox "There is insufficient data. Madatory fields must be added (*)", vbExclamation, "Mandatory fields are incomplete"
Exit Sub
End If
cNum = 3
For X = 1 To 3
If Me.Controls("Reg" & X).Value = "" Then
MsgBox "You must add all data"
Exit Sub
End If
Next
For X = 1 To cNum
nextrow = Me.Controls("Reg" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
'clear
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Next
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"
With ws
nextrow.Offset(0, 3).Value = Me.Reg1
nextrow.Offset(0, 4).Value = Me.Reg2
nextrow.Offset(0, 5).Value = Me.Reg3
End With
'sort the data
Sheet3.Select
With ws
ws.Range("C3:E10000").Sort Key1:=Range("C3"), Order1:=xlAscending, Header:=xlGuess
End With
'return to sheet
Sheet3.Select
'reset the form
Unload Me
AddDB1_UF.Show
'update the sheet
Application.ScreenUpdating = True
'clear the values
With Me
.Reg1.Value = ""
.Reg2.Value = ""
.Reg3.Value = ""
End With
MsgBox "The data has been sent to the database"
End Sub
Last edited: