Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA - form controls to reference programatically

Posted on 2016-11-13
5
Medium Priority
?
130 Views
Last Modified: 2016-11-14
I have a UserForm with several pairs of CheckBoxes and ComboBoxes.

I'd like to write (just) one block of code to make the following happen.

When CheckBox 'chk1' is clicked I want the ComboBox 'cmb1' to become visible.

I would imagine step 1 would be to determine which CheckBox was clicked. I tried Display.Caller but it didn't work (I got a type mismatch error). Step 2 I think would be to reference the control with suffix '1', having picked up the '1' in the control that was clicked. I'm stuck on that.

Thanks for any help.
0
Comment
Question by:hindersaliva
  • 3
  • 2
5 Comments
 
LVL 49

Assisted Solution

by:Martin Liss
Martin Liss earned 2000 total points
ID: 41885520
If the code is in the userform then try Activecontol.Name.
0
 

Author Comment

by:hindersaliva
ID: 41885531
Martin, that works.
So I can find which number it is with Right(ActiveControl.Name, 1) which is '1'.

Now to reference the ComboBox cmb1 ....

"cmb"+ Right(ActiveControl.Name, 1)
How do I put it into Me.??????????.Visible = True
for ???????? to be 'cmb1'?
0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 41885533
Dim ctl As Control

For Each ctl In Controls
    If Right(ActiveControl.Name, 1)   = "1" Then
        ctl.Visible = True
        Exit For
    End If
Next

Open in new window

0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41885541
I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0
 

Author Comment

by:hindersaliva
ID: 41886344
On testing it I made this small change to make it work.

Dim ctl As Control

For Each ctl In Controls
    If Right(ctl .Name, 1)   = "1" Then       'not ActiveControl.Name
        ctl.Visible = True
        Exit For
    End If
Next

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question