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
Solved

Excel VBA - form controls to reference programatically

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

840 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