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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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