Search code examples
excelvbainputbox

Inputbox prevent user from pressing enter when value is blank


I have the following code : Range("P" & Rows.Count).End(xlUp).offset(1).Value = InputBox("INTRODUCE ACT JUSTIFICATIV") User must enter letters in inputbox

I want to prevent the user from accidentally pressing enter if value in inputbox is blank with a personalized error message.

And pressing cancel button to exit sub.


Solution

  • Set your Users input to a variable and then test to see whether it is valid or not.

    Dim usrInput as String
    
    Do
        usrInput = InputBox("INTRODUCE ACT JUSTIFICATIV")
    
        If usrInput = vbNullString Then
            MsgBox "Your Error Message"
        End If
    Loop While usrInput = vbNullString