VBA code to check if any of a range of controls are blank

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have done some sniffing around to see if I can find solution to my problem. I came close but needed a hand to finish it up for me.

Code:
[COLOR=#101094][FONT=monospace]Dim[/FONT][/COLOR][COLOR=#303336][FONT=monospace] allEmpty [/FONT][/COLOR][COLOR=#101094][FONT=monospace]As [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Boolean
[/FONT][/COLOR][COLOR=#303336][FONT=monospace]allEmpty [/FONT][/COLOR][COLOR=#303336][FONT=monospace]=[/FONT][/COLOR][COLOR=#7D2727][FONT=monospace]True
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]For [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Each[/FONT][/COLOR][COLOR=#303336][FONT=monospace] crtl [/FONT][/COLOR][COLOR=#101094][FONT=monospace]In [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Me[/FONT][/COLOR][COLOR=#303336][FONT=monospace].[/FONT][/COLOR][COLOR=#303336][FONT=monospace]Controls    
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]    If[/FONT][/COLOR][COLOR=#303336][FONT=monospace] crtl[/FONT][/COLOR][COLOR=#303336][FONT=monospace].[/FONT][/COLOR][COLOR=#303336][FONT=monospace]Name [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Like [/FONT][/COLOR][COLOR=#7D2727][FONT=monospace]"txtM*" [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Then
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]       If[/FONT][/COLOR][COLOR=#303336][FONT=monospace] Trim[/FONT][/COLOR][COLOR=#303336][FONT=monospace]([/FONT][/COLOR][COLOR=#303336][FONT=monospace]crtl[/FONT][/COLOR][COLOR=#303336][FONT=monospace].[/FONT][/COLOR][COLOR=#303336][FONT=monospace]Value[/FONT][/COLOR][COLOR=#303336][FONT=monospace]) [/FONT][/COLOR][COLOR=#303336][FONT=monospace]<> [/FONT][/COLOR][COLOR=#7D2727][FONT=monospace]"" [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Then[/FONT][/COLOR][COLOR=#303336][FONT=monospace]           
         allEmpty [/FONT][/COLOR][COLOR=#303336][FONT=monospace]=[/FONT][/COLOR][COLOR=#7D2727][FONT=monospace]False
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]       Exit [/FONT][/COLOR][COLOR=#101094][FONT=monospace]For
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]     End [/FONT][/COLOR][COLOR=#101094][FONT=monospace]If
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]     End [/FONT][/COLOR][COLOR=#101094][FONT=monospace]If
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]Next
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]If[/FONT][/COLOR][COLOR=#303336][FONT=monospace] allEmpty [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Then[/FONT][/COLOR][COLOR=#303336][FONT=monospace]    
   MsgBox [/FONT][/COLOR][COLOR=#7D2727][FONT=monospace]"dont forget .... blablabla"
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]   Exit [/FONT][/COLOR][COLOR=#101094][FONT=monospace]Sub
[/FONT][/COLOR][COLOR=#101094][FONT=monospace]End [/FONT][/COLOR][COLOR=#101094][FONT=monospace]If[/FONT][/COLOR]

What I want to do here, is to check a given range of controls.

My controls are named "reg1" to say "reg20".

But I want to see if from reg4 to reg8, has any blank. The code I see so far all check for all controls. I need just a range

Can someone pull that string up for me?
Thanks in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
Code:
    Dim i As Long
    For i = 4 To 8
        If Me.Controls("reg" & i) <> "" Then
            allEmpty = False
            Exit Sub
        End If
    Next i
 
Upvote 0
How about
Code:
    Dim i As Long
    For i = 4 To 8
        If Me.Controls("reg" & i) <> "" Then
            allEmpty = False
            Exit Sub
        End If
    Next i


Okay. Cooler than what I was thinking.

So what if I want to test if all is blank?

How will that be written?
 
Upvote 0
That's what it does, just replace your for each loop with my code
 
Upvote 0
That's what it does, just replace your for each loop with my code

Oh okay, the Len function was deceiving me. So I have to change it to make it work.

It's working now.

I appreciate that
 
Upvote 0
:confused:
the Len function was deceiving me. So I have to change it to make it work.
There is no LEN function.

But glad you got it working & thanks for the feedback
 
Upvote 0
:confused: There is no LEN function.

But glad you got it working & thanks for the feedback

Sure.

I was having another if condition with

If Len (regX)

It failed but when I used

If regX <> ""

This worked. That was the Len I was referring to.
:cool:
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

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