Search code examples
excelvbatextboxmsgbox

Warn if missing required informtion when passing Textbox entry to cell


I have a UserForm in Excel 16 that enters text from TextBox5 into cell Sheet1.Range("F2") when CommandButton1 is pressed.

I have default text in the box that says "End miles" in the TextBox.

I want to pop a message box up telling the user to enter their ending mileage in the box when they click the CommandButton without putting it in.

This is the relevant codes I am using now:

Private Sub UserForm_Initialize()
     If Not Range("F2").Text = "" Then TextBox5.Text = Range("F2").Text _
           Else TextBox5.Text = "End Miles"
End Sub

Private Sub CommandButton1_Click()
    Sheet1.Range("F2") = TextBox5.Text
    Unload Me
End Sub

I'm trying:

If Not TextBox5.Text = "End Miles" Then 
     MsgBox "Enter ending mileage" 
Else 
     Sheet1.Range("F2") = TextBox5.Text
End If

Solution

  • Your logic is reversed:

    If TextBox5.Text = "End Miles" Then 
         MsgBox "Enter ending mileage" 
    Else 
         Sheet1.Range("F2") = TextBox5.Text
    End If