Basic userform problem


New Member

I have a userform with 4-5 items (depending on the selected option) to put in and a submit button. The first box requires the user to add a 6 letter-ticket and a list of options for the remaining 4. Is there a way to halt the operation if the following criteria is met:

- if the first option has less than 6 letters or empty.
- in the event that a user selects the option that requires 5 data and leave the 5th data empty
- if the user leave any textbox empty.

Thank you in advance

Leith Ross

Well-known Member
Hello becklog,

If your questions are about TextBoxes on the SUser Form:

Q - if the first option has less than 6 letters or empty.
A - Set the TextBox MaxLength property to 6. This limits the input to a maximum of 6 characters only.

- in the event that a user selects the option that requires 5 data and leave the 5th data empty.
A - What are these Data? Where are they input to?

- if the user leaves any textbox empty.
A - You can check each TextBox Value property and pop up a message that it needs to be filled in before continuing.

Rich (BB code):
Rich (BB code):
' // Enter this code into the General Declarations Section of the UserForm

Dim Closing As Boolean

Function IsTextMissing() As Boolean
    If ActiveControl.Object.Value = "" And Not Closing Then
        MsgBox "Please Enter Text into this Field.", vbExclamation
        IsTextMissing = True
    End If
End Function

' // Add this code to each TextBox Exit event
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = IsTextMissing
End Sub

' // Add this code to the UserForm QueryClose Event
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Closing = True
End Sub

Last edited:

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...