VBA: Get the name of the clicked control


With a userform, I'd like to get the name of the clicked control automatically.
So for:

  Private Sub LeftHeaderBox2_Click()
    Set HeaderBox = Me.LeftHeaderBox2: Call FillProposalHeaderBox(HeaderBox)
   ' What can I replace 'Me.LeftHeaderBox2' with?
   ' Would think 'Me.ActiveControl' -- but 'Msgbox Me.ActiveControl.Name' names the frame that the
  '  label is in.

 End Sub

The current situation is only inconvenient because I have tons of clickable labels in my userforms.

Thanks for your help.

Who is Participating?
Antagony1960Connect With a Mentor Commented:
Labels can't get the focus so it follows that they can never be the active control. The simplest way to do what you want is to use a module-wide string variable which you change in each label's click event, like so:

Dim sHeader as String

Private Sub lblHeader1_Click()
    sHeader = lblHeader.Caption
End Sub

It's a pity VBA doesn't have control arrays a la VB proper as that would make it a piece of cake.
The only other method I can think of would rely on the labels being uniformly distributed and sized on their parent controls, in which case you could use the X and Y coordinates of the parent control's MouseDown event to calculate which label has been clicked on and set the string accordingly from a predefined array. But that might end up being as much work as setting the string in the labels' click events. And such a method would add a big overhead to the task if you ever needed to reposition the labels.
bishop3000Author Commented:
Hi Antagony1960,
Thanks for your feedback.
Definitely a moment of impotence for me...when the name of the control is in the procedure title itself but VBA won't tell me the control's name.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.