User Form - Issue (VBA)

Robby19

Board Regular
Joined
Mar 19, 2018
Messages
227
Hello!

So in response to a previous question, I went ahead and made a User Form. However, the skills that I am lacking make this one thing quite difficult.

I created the form and used the Date to display an error if the text box associated with the Date was not entered in a specific format. The problem that I am having is that even though I got the error message to pop-up, the User Form still adds the data to the spreadsheet. Thus when then user clicks "Ok" and fixes the date, now there are 2 entries in the spreadsheet vice the one.

Is there a way to not allow the user to "save" the user form and add the data to the spreadsheet if the date is formatted incorrectly?

For example;
[TABLE="width: 500"]
<tbody>[TR]
[TD]Account[/TD]
[TD]Date[/TD]
[TD]Ref #[/TD]
[/TR]
[TR]
[TD]111[/TD]
[TD]abc[/TD]
[TD]123[/TD]
[/TR]
[TR]
[TD]111[/TD]
[TD]01/01/2018[/TD]
[TD]123[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Private Sub cmdbtnSave_Click()
Dim emptyRow As Long
'Make Sheet3 active
 Sheet3.Activate
'Determine emptyRow
 emptyRow = WorksheetFunction.CountA(Range("B:B")) + 1
'Transfer information
 Cells(emptyRow, 2).Value = combAccount.Value
 Cells(emptyRow, 3).Value = txtDate.Value
 Cells(emptyRow, 4).Value = txtRef.Value
 Cells(emptyRow, 5).Value = txtPayee.Value
If optbtnYes.Value = True Then
     Cells(emptyRow, 6).Value = "Yes"
Else
     Cells(emptyRow, 6).Value = "No"
End If
 Cells(emptyRow, 7).Value = combCategory.Value
 Cells(emptyRow, 8).Value = txtMemo.Value
If combStatus.Value = "Reconciled" Then
     Cells(emptyRow, 9).Value = "R"
Else
    If combStatus.Value = "Cleared" Then
        Cells(emptyRow, 9).Value = "C"
Else
     Cells(emptyRow, 9).Value = "V"
End If
End If

If btnIncome.Value = False Then
     Cells(emptyRow, 10).Value = txtIncome
Else
     Cells(emptyRow, 11).Value = txtIncome
End If

If Not IsDate(Me.txtDate.Text) Then
    MsgBox "The Data entered is not a Date" & Chr(10) & "Please Try again"
End If
    
End Sub
 
Works beautifully!


Please suggestion helped.

Just be aware that you will get a type mismatch error if non numeric data is return from your textbox.
You may be able to manage this with the inclusion of the Val function which stops reading the string at the first character it is unable to recognize as part of a number.

Rich (BB code):
Cells(emptyrow, IIf(btnIncome.Value, 11, 10)).Value = CCur(Val(txtIncome.Value))

Dave
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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