How to pass an argument to the active control (vba)

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I want a way that I could pass an argument to the active or current control that I have selected.

For example, when I double click on a textbook, there is this code that I would run.

Instead of placing the code under each textbox, I want a way where I would create a separate Sub for the code then call it each time I double click a textbox.

The issue now is how to be able to know which textbox to point to from the sub.

All the codes that will run before pointing to the textbox does not depend on the various textboxes.

But they are a bit long - the reason why I want to avoid writing them one by one.

So say I double click textbox1 then after running all my verification code, what I do is unlock the textbox1 because it has been locked before I do the double click.

Is there a way around it?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If you assign a macro to a shape, you can get a reference to the shape using Activesheet.Shapes(application.caller).
 
Upvote 0
If you assign a macro to a shape, you can get a reference to the shape using Activesheet.Shapes(application.caller).
Hello,

I don't really understand your syntax properly so I came across this syntax somewhere :

Code:
Me.ActiveControl

But when I tried it gave me the name of the multi page form I used instead of the textbox I double click
 
Upvote 0
You didn’t specify where the text box was so I guessed a worksheet. Is your text box locked or disabled?
 
Upvote 0
You didn’t specify where the text box was so I guessed a worksheet. Is your text box locked or disabled?


The textbox is on a userform.

It could be it locked or unlocked.
But would always be enabled.

From the userform, I have a multi page form then I also have a frame on the page before the textbox

Code:
Me.ActiveControl.SelectedItem.ActiveControl

Got me to the frame

Then all my tricks disappeared
 
Upvote 0
You might use this UDF which drills down through Frames and MultiPages and returns the control that is active.
VBA Code:
' in userform's Code Module

Public Function ReallyActiveControl(Optional Container As Object) As MSForms.Control
    If Container Is Nothing Then
        Set Container = Me
    End If
   
    On Error Resume Next
    Set ReallyActiveControl = Container.ActiveControl
    Set ReallyActiveControl = Container
    On Error GoTo 0
   
    On Error Resume Next
    Do
        If TypeName(ReallyActiveControl) = "MultiPage" Then
            With ReallyActiveControl
                Set ReallyActiveControl = .Pages(.Value)
            End With
        End If
        Set ReallyActiveControl = ReallyActiveControl.ActiveControl
    Loop Until Err
    On Error Goto 0
End Function
 
Upvote 0


The udf is giving me the multi page form instead of the textbox

Check how I tested it
code.jpg
 
Upvote 0
I see one typo, the Pages property of a multipage is plural.

Rich (BB code):
If TypeName(ReallyActiveControl) = "MultiPage" Then
    With ReallyActiveControl
        Set ReallyActiveControl = .Pages(.Value)
    End With
End If
 
Upvote 0
I see one typo, the Pages property of a multipage is plural.

Rich (BB code):
If TypeName(ReallyActiveControl) = "MultiPage" Then
    With ReallyActiveControl
        Set ReallyActiveControl = .Pages(.Value)
    End With
End If
Sure, I fixed it later but still didn't get the right textbox I interacted with
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top