I have created a user form that allows users to add new records and, if the record is found, fill in the form with information from the active sheet. Now, I am trying to update the worksheet if the information was found. Thank you in advance.
Here is a copy of the code I am currently using:
Private Sub cmdAdd_click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("MH CXL's Working")
'requires account number to be entered
If Trim(Me.txtAcctNumber.Value) = "" Then
Me.txtAcctNumber.SetFocus
MsgBox "Please enter the account number"
Exit Sub
End If
Dim Acct As Variant
Acct = Me.txtAcctNumber.Value
'find first empty row in database
iRow = ws.Cells.find(what:="*", searchorder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtAcctNumber.Value
ws.Cells(iRow, 2).Value = Me.txtMemberName.Value
ws.Cells(iRow, 3).Value = Me.comboStatus.Value
ws.Cells(iRow, 4).Value = Me.comboAction.Value
ws.Cells(iRow, 5).Value = Me.txtDateCxlRec.Value
ws.Cells(iRow, 6).Value = Me.txtDateSigned.Value
ws.Cells(iRow, 7).Value = Me.comboRecommendAction.Value
ws.Cells(iRow, 8).Value = Me.comboRSType.Value
ws.Cells(iRow, 9).Value = Me.comboSalesPerson.Value
ws.Cells(iRow, 10).Value = Me.txtRB.Value
ws.Cells(iRow, 11).Value = Me.txtAR.Value
ws.Cells(iRow, 12).Value = Me.comboReasonCode.Value
ws.Cells(iRow, 13).Value = Me.txtCredit.Value
ws.Cells(iRow, 14).Value = Me.comboPlanner.Value
ws.Cells(iRow, 15).Value = Me.comboChargeback.Value
'let the user know it worked
MsgBox "Save Successful."
'clear the data
Me.txtAcctNumber.Value = ""
Me.txtMemberName.Value = ""
Me.comboStatus.Value = ""
Me.comboAction.Value = ""
Me.txtDateCxlRec.Value = ""
Me.txtDateSigned.Value = ""
Me.comboRecommendAction.Value = ""
Me.comboRSType.Value = ""
Me.comboSalesPerson.Value = ""
Me.txtRB.Value = ""
Me.txtAR.Value = ""
Me.comboReasonCode.Value = ""
Me.txtCredit.Value = ""
Me.comboPlanner.Value = ""
Me.comboChargeback.Value = ""
Me.txtAcctNumber.SetFocus
End Sub
Private Sub cmdClose_Click()
'closes form
Unload Me
End Sub
'upon exit of text box account number
Private Sub txtAcctNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'this variable is for error handling purposes
Dim rFound As range
Dim ws As Object
Dim Acct As Variant
'this is just to give rFound an initial value that we can test for
Set rFound = Nothing
Set ws = ThisWorkbook.Sheets(4)
Acct = Me.txtAcctNumber.Value
'do this because if the account number is not found, the ".Find" method will return an error.we don't want to throw an error if the account # isn't found, though. this Error Handling Statement will tell Excel to keep on going even if it gets an error.
On Error Resume Next
Set rFound = Cells.find(what:=txtAcctNumber, searchorder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues)
'this statement just turns normal error handling back on. We're not expecting any of the subsequent statements to throw an error, so we want Excel to alert us if they do.
On Error GoTo 0
'returns the values to the form
If Not rFound Is Nothing Then
Me.txtMemberName.Value = ws.Cells(rFound.Row, 2)
Me.comboStatus.Value = ws.Cells(rFound.Row, 3)
Me.comboAction.Value = ws.Cells(rFound.Row, 4)
Me.txtDateCxlRec.Value = ws.Cells(rFound.Row, 5)
Me.txtDateSigned.Value = ws.Cells(rFound.Row, 6)
Me.comboRecommendAction.Value = ws.Cells(rFound.Row, 7)
Me.comboRSType.Value = ws.Cells(rFound.Row, 8)
Me.comboSalesPerson.Value = ws.Cells(rFound.Row, 9)
Me.txtRB.Value = ws.Cells(rFound.Row, 10)
Me.txtAR.Value = ws.Cells(rFound.Row, 11)
Me.comboReasonCode.Value = ws.Cells(rFound.Row, 12)
Me.txtCredit.Value = ws.Cells(rFound.Row, 13)
Me.comboPlanner.Value = ws.Cells(rFound.Row, 14)
Me.comboChargeback.Value = ws.Cells(rFound.Row, 15)
Else
Me.txtAcctNumber.SetFocus
End If
End Sub
Moderator's Note: Welcome to the forum. You have to put code tags around codes. Select the code then hit "#" sign. I'll do it for you now. Thank you.
Bookmarks