Hi There,
I'm not sure if this is possible, but what I'm trying to do is create an error message that will prevent a user selecting the command button in a userform if a text bos is empty.
I've attached a spreadsheet with a simple, example userform. What I would like to do is stop the user adding the record if the "service date" text box is empty. Idealy I would like an error message to display something like "please enter service date". Is this possible to do?
Below is the code.
Option Explicit
Private Sub Auto_Open()
UserForm1.Show
End Sub
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim Rng As Range
Set ws = Worksheets("DataSource")
Set Rng = ws.Range("A2")
iRow = ws.Cells(Rows.Count, Rng.Column).End(xlUp).Row
iRow = IIf(iRow < Rng.Row, Rng.Row, iRow + 1)
ws.Cells(iRow, 1) = name1.Value
ws.Cells(iRow, 2) = Vehicle.Value
ws.Cells(iRow, 3) = date1.Value
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Worksheets("DataSource")
Dim iRow As Long
iRow = Selection.Row
UserForm.name1.Text = ws.Cells(iRow, 1)
UserForm.Vehicle.Text = ws.Cells(iRow, 2)
UserForm.date1.Text = ws.Cells(iRow, 3)
End Sub
Bookmarks