Private Sub CommandButton1_Click()
'enter UserForm data in worksheet
Dim totalRows As Long
Dim str As String
If TextBox1.Text = "" Then
MsgBox "Please Enter the Transaction Date", vbOKOnly, "Date Error!"
Exit Sub
ElseIf ComboBox1.Value = "" Then
MsgBox "Please Select the Customer Name", vbOKOnly, "CustomerName Error!"
Exit Sub
End If
If TextBox3.Text = "" Then
MsgBox "Please Enter the Debit Amount", vbOKOnly, "Debit Error!"
Exit Sub
End If
'determine worksheet row to post data
totalRows = Sheet4.Cells(Rows.Count, "A").End(xlUp).Row
If totalRows < 5 Then
totalRows = 5
Else
totalRows = totalRows
End If
'Post Data in worksheet:
Sheet4.Cells(totalRows + 1, 1) = TextBox1.Text
Sheet4.Cells(totalRows + 1, 2) = ComboBox1.Value
Sheet4.Cells(totalRows + 1, 5) = TextBox3.Text
MsgBox "Data Added Sucessfully!", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.ComboBox1 = ""
Me.TextBox2 = ""
Me.TextBox3 = ""
End Sub
Private Sub CommandButton2_Click()
Dim iCancle As VbMsgBoxResult
iCancel = MsgBox("Confirm if you want to Cancel", vbQuestion + vbYesNo, "Transaction Form")
If iCancel = vbYes Then
Unload Me
End If
End Sub
Private Sub TextBox3_Change()
TextBox3.Value = Format(TextBox3.Value, "#N###,##")
End Sub
Private Sub UserForm_Initialize()
Dim i As Long, LastRow As Long, ws As Worksheet
Set ws = Sheets("Customers")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
Me.ComboBox1.AddItem ws.Cells(i, "C").Value
Next i
Set ws = Sheets("Banking")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Val(Me.ComboBox1.Value) = ws.Cells(i, "B") Then
Me.TextBox2 = ws.Cells(i, "D").Value
End If
Next i
TextBox1.Text = Format(Now(), "dd mmm, yyyy")
End Sub