Return active OLEObject in Worksheet

  • Hello,


    I am trying figure out a way of returning the currently active OLEObject in a worksheet. My goal is to allow tabbing between text and combo boxes in a specific order, which is currently specified in an array. To do this, I'm using a simple piece of code

    Code
    If KeyCode = 9 Then .OLEObjects(objArray(objArrayActive)).Activate

    with "objArray" being a list of the names of all objects I want to be accessible, and objArrayActive being a numerical value representing the item I wish to activate. My thought was that I could simple increment / decrement from the currently active object.


    So far I can't find any way of returning the active OLEObject, unless I'm working in a form (which I really want to avoid).


    I would also like to be able to capture a "_KeyDown" event taking place in any object on the sheet without having to have a new "ComboBox_KeyDown" subroutine for every object. "Worksheet_KeyDown..." doesn't seem do do this.


    Thanks for the help!


    Jonathon

  • Re: Return active OLEObject in Worksheet


    I have a series of drop down lists, which are populated serially based on user selections. Also it's more flexible and looks cleaner.

  • Re: Return active OLEObject in Worksheet


    You Can make use of Application.Onkeys.
    Application.OnKey "{TAB}","KeyCapturing"
    Application.OnKey "{ESC}","Rangeselect"


    In the first on keys whenever a user press tab button the KeyCaputring macor will be called. and in case of second onkeys event rangeselect will be called.

  • Re: Return active OLEObject in Worksheet


    Quote from kulshresthazone;671805

    Application.OnKey "{TAB}","KeyCapturing"


    That could work for me to tab to the next object, but I think I still need to be able to find out what object is curently active. As mentioned before, I have an array which lists the objects I want to tab between and in a specific order. If I know what my current object is, I can activate the next or previous object pretty easily.

  • I know this is a very very late response. But I wish it helps someone who stumbles upon this page with the same frustration.


    Don't be put off by the length of what is written here. Not complicated like it appears. Simple to understand with a bit of patience. And it will save tons of time.


    Solution: The way is to create two Class Modules (one for TextBox KeyDown event and one for ComboBox KeyDown event) and set all textboxes and comboboxes to these classes. The class module will pass the name of active oleObject to the subroutine you wish to run.


    Also like Jonathon required, we could write a subroutine inside the keydown event of these class modules just once and that would apply to all textboxes and comboboxes in the worksheet. This saves the time of writing the same codes for KeyDown event of all textboxes and comboboxes separately.


    The above classes would determine which oleObject is active and pass that to the subroutine you want to run using that information.


    Solution Step by Step


    STEP 1


    I will first write the subroutine that finally runs - the one that controls tabbing between textboxes and comboboxes using the information about the active oleObject.


    Assumed that array is already in place.


    Here we loop through the array to find the position of active oleObject in the array and jump to the next oleObject in the array. The name of the active oleObject is passed as parameter to this subroutine by the class modules which we create.


    STEP 2


    Create a Class Module for TextBox (Insert > Class Module) .


    I am naming it as ClsTxtBx (Click on the created Class, press F4 and type the name in the box to the right of 'Name' in the new Properties window that pops up if you do not have one already.)


    Inside the ClsTxtBx class module, type the following codes


    Code
    Option Explicit
    
    Public WithEvents TxtCtl As MSForms.TextBox


    Now select TxtCtl from the dropdown list on the left top of the module.


    Select the KeyDown event for the TxtCtl from the dropdown list on the right top of the module.


    Type the following codes for the KeyDown event


    Code
    Private Sub TxtCtl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        
            JumpToNextCtlfrmTxtBx TxtCtl
    
    all subroutines we place here will run for the keydown event of all textboxes.
    
    End Sub


    STEP 3


    Create a Class Module for ComboBox (Insert > Class Module) .


    I am naming it as ClsComBx (Click on the created Class, press F4 and type the name in the box to the right of 'Name' in the new Properties window that pops up if you do not have one already.)


    Inside the ClsComBx class module, type the following codes


    Code
    Option Explicit
    
    Public WithEvents ComboCtl As MSForms.ComboBox


    Now select ComboCtl from the dropdown list on the left top of the module.


    Select the KeyDown event for the TxtCtl from the dropdown list on the right top of the module.


    Type the following codes for the KeyDown event


    Code
    Private Sub ComboCtl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        
            JumpToNextCtlfrmComBx ComboCtl
    
    all subroutines we place here will run for keydown event of all comboboxes.
    
    End Sub


    STEP 4


    Place the following codes in the Open event of the workbook.


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!