Solved

Excel VBA - form controls to reference programatically

Posted on 2016-11-13
5
38 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 46

Assisted Solution

by:Martin Liss
Martin Liss earned 500 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 46

Accepted Solution

by:
Martin Liss earned 500 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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now