Sri Mallikarjun
Sri Mallikarjun

Reputation: 3

How to check if all TextBoxes on an Excel UserForm are filled?

I have bunch of input TextBoxes. I need to check all TextBoxes are filled before processing. Since I have 17 TextBoxes, can any one help me to write a code to check all.

I have tried by using If ... Then ... Else but it seems not working in my case.

user form

Upvotes: 0

Views: 3363

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57743

You can …

  1. … loop through all controls

    For Each Ctrl In Me.Controls
    
  2. … then check if the current control is a TextBox

    If TypeName(Ctrl) = "TextBox" Then
    
  3. … and then check if it is empty

    If Ctrl.Text = vbNullString Then
    

So you end up with something like this:

Option Explicit

Public Sub ValidateCheckBoxes()
    Dim EmptyBoxesFound As Boolean

    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "TextBox" Then
            If Ctrl.Text = vbNullString Then
                EmptyBoxesFound = True
                Exit For
            End If
        End If
    Next Ctrl

    If EmptyBoxesFound Then
        MsgBox "At least one box is not filled.", vbExclamation
    Else
        MsgBox "All boxes are filled.", vbInformation
    End If
End Sub

Upvotes: 4

Related Questions