1

This is some what of an opinion quest, but it is regarding excel vba events

I have a table that contains the history of our sales, but I want the user to be able to interact with each row in some way, so that it opens that sale in the editor.

As someone who comes from Javascript, my first thoughts were click or double click events on the row cells, but in excel these events already have their functions, so its not very intuitive

What is an "excel-like" way that I can achieve this? Or, what are my options?

Anyone that tried this before might have a successful method to share?

Or is it okay to add a button to each row? There are 2.000 + rows and growing

2
  • 1
    When I had to do something similar, I provided two options to the users. (1) I added an additional option to the right-click menu for the cell, which called my subroutine (2) I added a button to the worksheet that called my subroutine. Obviously the subroutine then used the ActiveCell object to determine which line of the table was being referred to.
    – YowE3K
    Aug 15, 2017 at 20:40
  • @YowE3K Oh I didn't know you could add new options in the right click context menu, that sounds perfect
    – Mojimi
    Aug 15, 2017 at 20:42

2 Answers 2

2

I use a double click event. The VBA code is placed in the worksheet so it is local to that worksheet.

The worksheet has an event handler like so:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    'Put your code here
    'The Target parameter will tell you which cell was double clicked.
    'Target.Row will be the 1 based row number.
    'Target.Column will be the 1 based column number.
    'Target.Address will give you the cell address in "A1" format

    'The Cancel parameter is a return value. 
    'If you set it to true, Excel will "cancel" or ignore the double click.
End Sub

As an example I have a sheet that does file seraches. Cell A1 is input for the file search. Cell B1 is the input for text to find in the file. Row 2 is just labels but I use them for sorting the found data. Double clicking on either A2 or B2 sorts from row 3 down. The sort sequence reverses each time a cell on row 2 is double clicked. So the the first double click on A2 sorts the sheet by ascending file name and the second double click on A2 sorts descending by file name. Ditto for B2 and the paths.

Rows 3 and following receive the search results. Column A receives the file name. Column B receives the path to the file. Double clicking on a file name in Column A will open the file if it has an associated program. Double clicking on a path in Column B will open Windows Explorer at that folder.

If the Target parameter indicates a double click on an empty cell, I set Cancel to True and exit, resulting in no action taken.

It gives you more granularity than you are requesting but that's just an example. It sounds like you only need the row.

1

Sample code that uses the right-click context menu and adds links to two subroutines (GetFields and GetTables):

Code in ThisWorkbook code module:

Private Sub Workbook_Activate()
    Call AddToCellMenu
End Sub

Private Sub Workbook_Deactivate()
    Call DeleteFromCellMenu
End Sub

Code in a standard code module:

Option Explicit

Sub AddToCellMenu()
    Dim ContextMenu As CommandBar
    Dim MySubMenu As CommandBarControl

    ' Delete the controls first to avoid duplicates.
    Call DeleteFromCellMenu

    ' Set ContextMenu to the Cell context menu.
    Set ContextMenu = Application.CommandBars("Cell")

    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "GetFields"
        .FaceId = 498
        .Caption = "Get Field Names"
        .Tag = "My_Cell_Control_Tag"
    End With
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "GetTables"
        .FaceId = 585
        .Caption = "Get Table Names"
        .Tag = "My_Cell_Control_Tag"
    End With

    ' Add a separator to the Cell context menu.
    ContextMenu.Controls(3).BeginGroup = True
End Sub

Sub DeleteFromCellMenu()
    Dim ContextMenu As CommandBar
    Dim ctrl As CommandBarControl

    ' Set ContextMenu to the Cell context menu.
    Set ContextMenu = Application.CommandBars("Cell")

    ' Delete the custom controls with the Tag : My_Cell_Control_Tag.
    For Each ctrl In ContextMenu.Controls
        If ctrl.Tag = "My_Cell_Control_Tag" Then
            ctrl.Delete
        End If
    Next ctrl

    ' Delete the custom built-in Save button.
    On Error Resume Next
    ContextMenu.FindControl(ID:=3).Delete
    On Error GoTo 0
End Sub

See also: Adding Controls to the Cell Context Menu by Using VBA Code (which is probably where I originally got the code from)

Note: The FaceId property is fun to play with. I forget now where I obtained the list of what icon each value referred to. (It might have been from one of the links on the MSDN page.) Edit: I doubt if this is where I originally saw it, but this Stack Overflow answer by Horst Schmid is probably useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.