Solved

Excel VBA - form controls to reference programatically

Posted on 2016-11-13
5
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 47

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 47

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 47

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

734 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