Getting Loop in Excel VBA to validate only certain textboxes

DawidV

New Member
Joined
Jul 14, 2016
Messages
29
I have been able to make my Loop to look for Empty TextBoxes on my userform with 5 Tabs and it is working up to a fashion. Not as I want it to work. I have 23 textbox to be completed. I have written individual code for each TextBox to validate but that is a lot of code!!! thought a Loop will work better.

My help call is the following:

I am trying to change the ForeColor as well if the Textbox is empty string. The Backcolor if the textbox is empty os working fine.
I am trying to SetFocus to the first textbox that has empty string. I think it has something to do with the fact that I have 5 Tabs and a lot of different Frames on the Tabs.

This is the code for the Loop.
I have also included some of the code which I originally wrote to do the validating for each individual textbox. there are 23 of them.


PLEASE I need help seriously.

<pre>

Private Function EverythingFilledIn() As Boolean
'CREATING FUNCTION TO STEAMLINE CODE TO USE DURING VALIDATION OF CONTROLS ON USERFORM
'MAKES CODE ESIER TO READ AND MORE FLEXIBLE TO MAKE CHANGES TO
'LOOP OVER ALL CONTROLS ON THE FORM INSTEAD OF ONE BY ONE

Dim ctl As MSForms.Control
'set variable that will look at every single Control on the form be it Text, Date, ComboBox etc use MSForms.Control as this will make certian that we always use contols from MSForms contol - to avoid duplication from other controls
Dim txt As MSForms.TextBox
'variable so thaty code can be made more robust and to make changes so that the intellisense will work
Dim AnythingMissing As Boolean
'to find if anything is missing in textbox to SetFocus to that textbox

'assume everything on userform has been filled in for procedure to work properly
EverythingFilledIn = True
AnythingMissing = False

For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.TextBox Then
Set txt = ctl 'transfer what is in the ctl variable to txt variable - will make intellisece work as it should
If txt.Value = "" Then 'check if Contols value is an empty string
txt.BackColor = rgbHotPink 'if Contols value is an empty string change Backcolor to Pink
'Controls(ctl.Name & "lbl").ForeColor = rgbFireBrick
If Not AnythingMissing Then ctl.SetFocus
AnythingMissing = True
EverythingFilledIn = False
End If
End If
Next ctl
End Function
</pre>

<pre>
Private Function EverythingFilledIn() As Boolean
'CREATING FUNCTION TO STEAMLINE CODE TO USE DURING VALIDATION OF CONTROLS ON USERFORM
'MAKES CODE ESIER TO READ AND MORE FLEXIBLE TO MAKE CHANGES TO
'LOOP OVER ALL CONTROLS ON THE FORM INSTEAD OF ONE BY ONE

Dim ctl As MSForms.Control
'set variable that will look at every single Control on the form be it Text, Date, ComboBox etc use MSForms.Control as this will make certian that we always use contols from MSForms contol - to avoid duplication from other controls
Dim txt As MSForms.TextBox
'variable so thaty code can be made more robust and to make changes so that the intellisense will work
Dim AnythingMissing As Boolean
'to find if anything is missing in textbox to SetFocus to that textbox

'assume everything on userform has been filled in for procedure to work properly
EverythingFilledIn = True
AnythingMissing = False

For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.TextBox Then
Set txt = ctl 'transfer what is in the ctl variable to txt variable - will make intellisece work as it should
If txt.Value = "" Then 'check if Contols value is an empty string
txt.BackColor = rgbHotPink 'if Contols value is an empty string change Backcolor to Pink
'Controls(ctl.Name & "lbl").ForeColor = rgbFireBrick
If Not AnythingMissing Then ctl.SetFocus
AnythingMissing = True
EverythingFilledIn = False
End If
End If
Next ctl


End Function
</pre>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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