Can I refer to Checkbox1 using "Checkbox & i" (where i=1) ?

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
Hi everyone,

This is something I've struggled with for a long time. It's proving quite difficult to google so I've come here.

I have a form with countless checkboxes, all neatly named (Checkbox1, Checkbox2 etc).

A subroutine loads this form and assigns values (i.e. true/false) to these checkboxes before showing it, based on various conditions. However it is immensely wasteful to put an "IF THEN" line of code for each and every one... is there a way I can loop through them, using the pattern in their control names? Can it be done for variables?

Thanks!

Here's a (very) simplified example of my code:

Code:
If integer1 = 7 then .Checkbox1 = True
If integer2 = 7 then .Checkbox2 = True
If integer3 = 7 then .Checkbox3 = True
If integer4 = 7 then .Checkbox4 = True

And here is an example of what I'm trying to do (except obviously this code doesn't work):

Code:
For i = 1 To 4
If integer & i = 7 then .Checkbox & i = True
Next i
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

I think you need to do 2 things:

1. Define your variable integer1, integer2 etc as an array; something like this will do: dim nInteger(5) as integer note I've changed the name to nInteger as 'integer' itself is a reserved word.
2. Your code should then look like this:

Code:
For n = 1 To 4
    If nInteger(n) = 7 Then UserForm1.Controls("Checkbox" & n) = True
Next

Hope this helps.

Regards
 
Upvote 0

Forum statistics

Threads
1,203,097
Messages
6,053,519
Members
444,669
Latest member
Renarian

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