Looping UserForm controls

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,126
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All

I'm trying to make my code a little more compact

Take the example below
I have loads of comboboxes to cycle through, well, about 36 and growing, now i could write the below out 36 times, but

VBA Code:
    If Me.C1 <> "" Then
        If Me.C1C = "" Or Me.C1CC = "" Then MsgBox "MISSING INFOR A CRITERIA 1"
    End If
    If Me.C2 <> "" Then
        If Me.C2C = "" Or Me.C2CC = "" Then MsgBox "MISSING INFOR A CRITERIA 2"
    End If

could i not just loop like the below??
If i can i cannot work out how to actually write it.

VBA Code:
dim a(1 to 36) as integer
for a = 1 to 36
    If Me.C(a) <> "" Then
        If Me.C(a)C = "" Or Me.C(a)CC = "" Then MsgBox "MISSING INFOR A CRITERIA " & a
    End If
next a

thanks for your help

Dave
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Explain further...
• if these comboboxes are embedded onto a worksheet or are in a userform.
• if embedded onto a worksheet, clarify if they are Form control or ActiveX controls.
• what C1C and C2C and C2CC mean for example.
Hi Tom, thanks for the reply.

These combo boxes are on a user form(in a frame)
i have
36 combo boxes c1-c2-c3-c4 and so on
36 combo boxes c1c-c2c-c3c-c4c and so on
36 combo boxes c1cc-c2cc-c3cc-c4cc and so on

I'm not against re-naming them if its easier

i have more useform to make going forward as well, so not to keep to keep doing it longhand.

thanks

Dave
 
Upvote 0
You'd use Me.Controls("C" & a) and similarly for the other two controls.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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