Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Object Name Syntax

Status
Not open for further replies.

osx99

Technical User
Apr 9, 2003
250
GB
In excel I have a combobox from the control toolbox menu named 'H1_1'

I have an event which runs from Private Sub H1_1_gotfocus()

What is the correct syntax to capture the object name?

Eg this doesn't work
Code:
Private Sub H1_1_gotfocus()

MsgBox (screen.ActiveControl.Name)

end sub

thank you
os
 
You can use ActiveControl in an UserForm.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
So what do I use instead for a control toolbox object not in a userform?
 
It might be better to tell us what you're trying to achieve. The reason I say this is that in the code you posted the control with focus will be H1_1 as you are using the gotfocus event, as this is the case there is no real call to dynamically find the control's name.

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Ok, all I want to do is avoid duplication of code which have identical events. So for instance Comboboxes H1_1 to H_20 will all do the same thing where the event code for H1_1 is

Code:
Private Sub H1_1_gotfocus()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim i As Long

Me.H1_1.List = Worksheets("Test").Range("da1:db100").Value

For i = H1_1.ListCount - 1 To 0 Step -1
   If H1_1.List(i, 0) = "" Or Len(H1_1.List(i, 1)) <> 2 Then
    With H1_1
     .RemoveItem (i)
   
     End With
   End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

I was trying to find a way to pass the active combobox name so I only have to write this once a call some generic code

Something along the lines of

Private Sub H1_1_gotfocus()
genriccode(comboxbox.name)
end sub

Private Sub H1_2_gotfocus()
genriccode(comboxbox.name)
end sub

etc...

 
Simply this ?
Private Sub H1_1_gotfocus()
genriccode("H1_1")
end sub

Private Sub H1_2_gotfocus()
genriccode("H1_2")
end sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Alternatively you can pass the control (combobox) as argument:
Code:
Private Sub H1_1_gotfocus()
genriccode(Me.H1_1)
end sub
Code:
Sub genricode(obj As MSForms.ComboBox)
' Obj.List 'reference to list
' Obj.Parent ' reference to host worksheet
End Sub

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top