Checking all textbox controls on userform for null value

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
Need a little help here...got stuck.

I'm checking all the textboxes on a userform for a null value which is working fine, but need to now exit the sub if one or more are null.

The code below gives the name of the textbox that is null, but even if everything is not null I still get the msgbox.

Any thoughts?

Code:
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text = "" Then
                txtAddress = txtAddress & ctrl.Name & " is empty" & vbNewLine
            End If
        End If
    Next ctrl
MsgBox txtAddress, vbOKOnly, "Checking textboxes for no entry"
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe try:

Code:
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text = "" Then
                txtAddress = txtAddress & ctrl.Name & " is empty" & vbNewLine
                Exit For
            End If
        End If
    Next ctrl
    If txtAddress <> "" Then
        MsgBox txtAddress, vbOKOnly, "Checking textboxes for no entry"
    End If
 
Upvote 0
Hi Andrew and thanks for the help,

That doesn't seem to do it. I have multiple textboxes on userform1.

I filled in one textbox selected the command button to enter data to the worksheet and I get a msgbox saying one textbox is empty (more than one are actually empty), but then it still writes the info from the one filled out textbox to the worksheet.

I would like it to list all textboxes which are empty and then exit the sub or if all textboxes are not empty then for the entered values to be written to the worksheet.
 
Upvote 0
You haven't posted any code that transfers data to a worksheet, but maybe what you want is:

Code:
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text = "" Then
                txtAddress = txtAddress & ctrl.Name & " is empty" & vbNewLine
            End If
        End If
    Next ctrl
    If txtAddress <> "" Then
        MsgBox txtAddress, vbOKOnly, "Checking textboxes for no entry"
        Exit Sub
    End If
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top