I am trying to create a custom context menu in the formula bar (the one that appears when you right-click when you are inside a cell by typing, for example). I managed to get the button that I want by right-clicking, but I can not find how to determine where the cursor is inside the cell.
For example, if the cursor is after the 5th character in a cell, how can I determine what happens before or during the BeforeRightClick event? The only thing I found is the opposite problem; placing the cursor in a specific part of the cell using SendKeys:
'=================================================================================== '- MOVE CURSOR IN THE FORMULA BAR AS REQUIRED '- Brian Baulsom September 2007 '=================================================================================== Sub test() Dim c As Integer c = 3 '------------------------------------------------------------------------------ Range("A1").Select ' select cell SendKeys "{F2}", True ' activate formula bar. cursor is to right of contents. DoEvents '------------------------------------------------------------------------------ '- move cursor 3 characters to the left in the formula bar '- or in the cell if Tools/Options/Edit .. "Edit directly in cell" is checked SendKeys "{LEFT " & CStr(c) & "}", True ' ="{LEFT 3}" DoEvents End Sub '----------------------------------------------------------------------------------- source: http:
The ultimate goal is to insert some standard substrings as you type into the cell. For example, I would like to insert [hi this is standard string X-XXX-XXXX] , right-clicking as you type into the cell.
EDIT:
I tried using sendkeys to send my string directly, but I just get the "ping" sound from Excel, which indicates that this is not possible. This was what I tried:
With fbar.Controls.add(Temporary:=True, Type:=msoControlButton, Before:=1) .BeginGroup = False .FaceId = 267 .Caption = wsLabels.GetLabel("rcRefMoM") .OnAction = "'" & ThisWorkbook.Name & "'!'rcAddRef2 '" End With Function rcAddRef2() SendKeys (" [Ref:X \NAME]") End Function
EDIT 2: Actually, it doesnβt even enter my rcAddRef2 function, it just pings directly, because I'm in edit mode. It does not activate a breakpoint if I put it there. So Sendkeys may not be as much a problem as running a function.
It works fine in the Cell command bar if I try, so I'm a little at a loss:
'This runs With cbar.Controls.add(Temporary:=True, Type:=msoControlButton, Before:=1) .BeginGroup = False .FaceId = 267 .Caption = wsLabels.GetLabel("rcRefMoM") .OnAction = "'" & ThisWorkbook.Name & "'!'rcAddRef2 '" End With