Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Posted by Andrew Gould on 31 March 2016
Another aspect of validation is checking that every control on a form has been filled in. This video explains how to loop over the controls collection to check if every textbox has been completed.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
Profitable Films - Pt5 Validating Forms.xlsm | Excel workbook with macros |
Click to download a zipped copy of the above files.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | CodeGirl |
When: | 10 May 22 at 20:31 |
This code works for me; however, I have one textbox on my form that is for comments and isn't required to be filled in. How do exempt that textbox from being filled out? Additionally, I also have ComboBoxes that need to be filled in. How do I add them to this logic. TIA
From: | Andrew G |
When: | 11 May 22 at 07:04 |
Hi!
You can check for combo boxes using the same basic technique as for text boxes:
If TypeOf ctl Is MSForms.ComboBox Then
To exclude a specific text box you can test for some unique property which identifies it - its Name property is a good choice. If you have multiple controls you want to exclude from the validation you could enter a value in each control's Tag property, for example NoCheck, then test for that value in your loop. You can do that with nested If statements or by combining logical tests in a single statement:
If TypeOf ctl Is MSForms.TextBox And ctl.Tag <> "NoCheck" Then
I hope it helps!
From: | CodeGirl |
When: | 11 May 22 at 19:49 |
I got it to work using two separate For each statements. I tried two if statements and an elseif but both of these gave me an error of for without next. I am assuming the only way to turn the fields back to no highlight once the user enters data, before they hit the submit button, is to update each of the fields
From: | Andrew G |
When: | 13 May 22 at 08:58 |
Hi!
It's likely that you had the wrong number of End If statements. Here's the basic structure for a single For Each loop
Dim ctl As MSForms.Control
Dim tb As MSForms.TextBox
Dim cb As MSForms.ComboBox
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.TextBox Then
Set tb = ctl
If tb.Value = "" Then
tb.BackColor = rgbPink
End If
ElseIf TypeOf ctl Is MSForms.ComboBox Then
Set cb = ctl
If cb.Value = "" Then
cb.BackColor = rgbPink
End If
End If
Next ctl
You can use the Change event of a text box or combo box to detect when a user enters a value and switch the colour back but yes, you'd need to do this for each control.
From: | Candice Hulo |
When: | 07 Apr 17 at 21:07 |
First of all, THANK YOU for such brilliant quality videos! Andrew G. is the perfect balance of humor & guru! I have found each of these videos to be engaging, easily relatable and packed full of new information!
Onto my question...
I am building a userform to collect information into a workbook, very very similar to your examples with Films and the gross profits they made. HOWEVER, where your example had 3 input boxes, mine will have over 40 input boxes.
What would your recommendation be for an alternative to doing a similar action for each of the input boxes private subs? That is A LOT of repetiion and risk for missing an update. Is there a way to code the action once, but let it loop through each Private Sub?
Specifcially, I am trying to use the following code from the videos, but I dont want to copy paste and edit this over 40 times. The input boxes are labeled Criteria001 through Criteria045.
Private Sub AddToList_Button_Click()
If Criteria001.Value = "1" Or Criteria001.Value = "0" Or Criteria001.Value = "x" Then
MsgBox ProcessorName & "'s scores have been added to the Analyst Workbook."
Else
Criteria001.BackColor = rgbPink
Criteria001Label.ForeColor = rgbRed
Criteria001.SetFocus
MsgBox "You must enter either 1, 0, or x."
Exit Sub
End If
End Sub
From: | Andrew G |
When: | 10 Apr 17 at 07:33 |
Hi Candice,
Happy to hear that you've been finding the videos so useful! There certainly is a quicker way to vaildate multiple controls using a For Each loop. We do cover that technique in this video from around the 18:32 mark - this is definitely the approach to take if you hvae 40 text boxes to validate!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.