Userform Data Validation

vba_monkey

Board Regular
Joined
Dec 18, 2013
Messages
112
Hello,

I have a userfom with a large number of fields in it. Most but not all have some form of validation on the fields which turns the background colour red if an illegal value is entered e.g. invalid dates etc.

I need to find a way that to throw up an error message if the user clicks submit when any of the fields are coloured red.

Does anyone know how to do this please?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I would write something like this, but you will need to adjust it a little bit to your code...

Code:
Private Sub cmdSubmit_Click()
    
    Dim cCont As Control
    
    For Each cCont In Me.Controls
    
        If cCont.BackColor = rgbRed Then
            MsgBox "There are empty fields, pls fill in and try again"
            Exit Sub
        Else
            '.....your other code here

        End If
    Next cCont
    
End Sub
 
Upvote 0
Thank you, that works perfectly.

Well, actually there is a mistake in the above code, so you better use the one below:

Code:
Private Sub cmdSubmit_Click()
    
    Dim cCont As Control
    
    Dim i As Integer 'variable to count the fields in red as a number
    
    For Each cCont In Me.Controls
        If cCont.BackColor = rgbRed Then
            i = i + 1
        End If
    Next cCont
    
    
    If i > 0 Then
        MsgBox "Please fill in the fields in red and try again"
        Exit Sub
    Else
        'your other code here
    End If
    
End Sub
 
Last edited:
Upvote 0
Hi,

If you use the first code which I pasted, if the very first control (or field) for example doesn't have a red background color, the visual basic editor will step into the else block and execute whatever commands you have there...And I believe this is not what you want...You can debug this and you should see that it's not working as anticipated.

Whereaz in the second code, first, the loop will be performed and Excel will count all fields with red color. And only then, it will check if i>0 (so there is at least one empty / invalid field). In such case a message will be shown to the user and the sub will be completed.
If i=0, i.e. no empty/invalid fields, then the visual basic editor will complete whatever commands you have for your submit button.

I had such issue with one of my tools at work...

I hope it's understandable :)
 
Upvote 0
Can be simplified even further:

Code:
Private Sub cmdSubmit_Click()


Dim cCont As Control


    For Each cCont In Me.Controls
        If cCont.BackColor = rgbRed Then
           MsgBox "Please fill in the fields in red and try again"
           Exit Sub 'we have red so we failed
        End If
        
'your other code here


End Sub
 
Upvote 0
If I had three Userforms that I wanted it to check for red fields, would I need to repeat the above code each time for each form or can it be made to check all three at once?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
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