Use a variable in a reference to a control?

3xDave

New Member
Joined
Mar 9, 2009
Messages
2
I have been trying to figure this out off and on for many months now and there has to be a way.

Assume I have activex checkboxes named A1-A10.

This works:
Code:
Private Sub thisworks()
If Me.A1.Value = True Then
MsgBox "It's true"
End If
End Sub

This DOES NOT work:
Code:
Private Sub thisdoesnotwork()
myCtrl = "A1"
If Me(myCtrl).Value = True Then
MsgBox "It's true"
End If
End Sub

This is the only syntax I can find on the web after months of searching. I have tried all types of other ways but I am seriously missing something here. It would seem this type of variable referencing should be straightforward. No? LOL

The point is I want to use a loop such as this:
Code:
Private Sub usealoop()
For myCtrl = 1 to 10
 myCtrl = "A" & myCtrl
  If Me(myCtrl).Value = True Then
   MsgBox "It's true"
  End If
Next
End Sub

Help! Dave
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Private Sub usealoop()
For myCtrl = 1 to 10
  If Me.Controls("A" & myCtrl).Value Then
   MsgBox "It's true"
  End If
Next
End Sub
 
Upvote 0
Thanks for the answer and the welcome. I've been reading the board for a while and it's been very helpful. I'll be sure to contribute as well.

The answer from sous2817 worked perfectly. The activex checkbox is obviously a member of the OLEObjects collection and I was missing the .Objects reference when I had tried that collection some time ago.


The answer from xld did not work for me as I get a "Method or data member not found" and it highlights the .Controls portion of this line.

Thanks!!
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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