Trapping right-click in worksheet textbox - vba

I'd like to be able to trap the right-click event when a user right-clicks in a worksheet textbox not ActiveX.
I know it can be done easily for userform textboxes, that's not what I'm after.
In the worksheet event Worksheet_BeforeRightClick I have the following:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
mRightClick.RightClickOnMoMList rngTarget:=Target, boolCancel:=Cancel
End Sub
However it doesn't even enter it when I right click on the textbox (but a cell of the same worksheet does work). I suspect that it is due to the Target argument being a Range.
Is there a way to make that event trap right clicks on shapes like textboxes as well?

I've just done this:
- opened Excel and entered Design Mode
- added a text box to a worksheet
- double-clicked the text box, which took me to the Change event for that control
- I selected the MouseUp event
What you're after is Button = 2 for the right button (left button is 1).
So...
Private Sub TextBox1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
MsgBox "Left-click"
ElseIf Button = 2 Then
MsgBox "Right-click"
End If
End Sub

Related

How to init a macro on mousedown in MS Word in VBA?

In MS word, I'm trying to init a macro when a textbox is selected.
Textbox above is "Text Box 89". I can't find any specific name reference to it other than this.
https://learn.microsoft.com/en-us/office/vba/api/access.textbox.mousedown
Allow the same principle
Private Sub textbox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = acLeftButton Then
MsgBox "You pressed the left button."
End If
End Sub
On clicking the box, how do I init this macro?

Excel VBA Userform If Focus Changes Trigger

I have a couple user forms in VBA, id like to add a feature that when a user first clicks on a textbox (changes the focus to it), any text inside gets selected. I've seen this feature in some accounting applications and in your web browser when you first click the URL bar. Its essentially meant so that users can immediately overwrite a text field. Was wondering how I might do the same in VBA, but I'm still a novice. I looked through a couple sub triggers(dont know the correct term) but haven't seen any. I found one for when you click the text box but I don't want the text constantly being selected every time I click the field.
Thanks.
put this in your textbox event
Dim checked As Boolean
Private Sub TextBox1_Change()
If checked = True Then
TextBox1.SelStart = 0
TextBox1.SelLength = 0
checked = True
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
checked = False
End Sub
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If checked = False Then
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1)
checked = True
End If
End Sub
once you click your textbox, it will hightlight the text, if you click the text, it will unhightlight and allow you to modified the text. if you click outside that textbox and click back inside that will rehighlight the whole text.

How to close dropdown list in combobox On Mouse Move event in VBA?

I'm developing a user form in Access. I have this code to open dropdown menu when mouse is on combobox:
Private Sub cmbx_ID_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.cmbx_ID.SetFocus
Me.cmbx_ID.Dropdown
End Sub
But I want to close dropdown menu when mouse is away from the combobox (now to close the form he should either select an item in dropdown menu or click on the form). I found that I can create a button and close the form when mouse is on this button:
Private Sub Frame1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.SetFocus
SendKeys "{esc}", True
End Sub
but that not what I'd like.

Excel VBA - open activex control DTPicker1 within a sub

i have a date picker activex control in my worksheet. after a subroutine i would like the datepicker to open instead of having to rely on the user to navigate and click the box to open it
when i click 'view code' on the control i see it is names like this
Private Sub DTPicker1_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
End Sub
what would be the appropriate way to call/simulate this keydown event in another sub
example:
Sub ProcessResults
'do existing code
DTPicker1.KeyDown 'doesn't work - 424 object required
DTPicker1.Open 'doesn't work - 424 object required
DTPicker1.Activate 'doesn't work - 424 object required
DTPicker1_CallbackKeyDown 'doesn't work - sub or function not defined
End Sub
DTPicker1 itself works fine within the worksheet - like i said i just want to save a few clicks and have it open automatically at the end of a different sub
Focus DTPicker and do
SendKeys ("{F4}")

Create Formula in a TextBox using Excel VBA

Does anyone know if it's possible to have a textbox in a userform in Excel 2010 work like the formula editor?
In other words when the userform is up and the textbox is the focused control allow me to type say
=AND(
Then click cell D2 and have the text box then be
=AND($D$2
Then type a =
=AND($D$2=
Then click cell E2
=AND($D$2=$E$2
Then type )
=AND($D$2=$E$2)
I've played around with the RefEdit control but it just overwrites any custom text as soon as a range is selected on the sheet, I basically need it to append to the text box when I click on a range.
Put this code in the ThisWorkbook module:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo EndOfSub
If UserForm1.ActiveControl.Name = "TextBox1" Then
UserForm1.TextBox1.Value = UserForm1.TextBox1.Value & Target.Address
End If
EndOfSub:
End Sub
This way when your userform is loaded (UserForm1 here) and your textbox is active (TextBox1 here), the address of the selection is appended to it. If you need to add the worksheet's name too, change the 3rd line above:
UserForm1.TextBox1.Value = UserForm1.TextBox1.Value & sh.Name & "!" & Target.Address
If you need to use it on only one sheet, put the code to that Sheet's code instead of the ThisWorkbook's code.
It is not as sophisticated as the formula editor, as it always appends the selection. (Changing the just inserted reference is doable too if you need it, just takes a bit more checking.)
Do note that putting code into the ThisWorkbook's SheetChange method takes its toll: you will not be able to use Undo in this workbook.
A bit more elegant way to check if a Userform is loaded can be found (here)[http://www.ozgrid.com/forum/showthread.php?t=152892]. You can use this instead of the On Error Goto part.
Yes, this is possible in a TextBox, but the problem you have is that you'll need to show the UserForm modelessly because you need to select cells in a worksheet while the Userform is on show. If you want to do exactly as you describe, ie the very next keystroke is '=', then you will need to re-activate the UserForm otherwise you'll simply add an '=' into your selected cell.
I believe the Show command won't re-activate a modeless UserForm that is already on show and I'm not aware of an Activate command for a UserForm. Others may well know of one, but I'd use a couple of APIs to do the job. So the code in your Userform could be as follows (you may need to adjust the API declarations if you have Win64 and/or VB7 https://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx):
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" _
(ByVal hwnd As Long) As Long
Private mHwnd As Long
Private mTextBoxHasFocus As Boolean
Public Property Get TextBoxHasFocus()
TextBoxHasFocus = mTextBoxHasFocus
End Property
Public Sub AppendText(appendedText As String)
'Add the text to textbox
TextBox1.Text = TextBox1.Text & appendedText
'Activate this userform window
SetFocusAPI mHwnd
'API doesn't trigger UserForm_Activate event so call it
Me.ActivateByCode
End Sub
Private Sub TextBox1_Enter()
mTextBoxHasFocus = True
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
mTextBoxHasFocus = False
End Sub
Public Sub ActivateByCode()
'Set focus on any another control then back to textbox
btnSave.SetFocus
'Set cursor at end of text
TextBox1.SelStart = Len(TextBox1.Text)
TextBox1.SelLength = 0
TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
'Acquire the handle for this window
mHwnd = FindWindow("ThunderDFrame", Me.Caption)
End Sub
You'd then call the AppendText routine from a WorkSheet_Change event or, as in this case, a Workbook_SheetSelectionChange event (which caters for selections of a cell in any WorkSheet):
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If UserForm1.TextBoxHasFocus Then
UserForm1.AppendText Target.Address
'Uncomment the line below if you want sheet name as well as address
'UserForm1.AppendText Sh.Name & "!" & Target.Address
End If
End Sub
And remember to show your UserForm modelessly:
UserForm1.Show False
If you want to cursor to be in the TextBox when the UserForm opens, then add this line too:
UserForm1.ActivateByCode
(Posted on behalf of the question author).
Checking for the selection changed was what I was missing. I was SO focused on trying to make the UserForm behave I completely spaced on ALL the other events Excel has to work with!
In the end I went with a check in the SelectionChanged sub to see if a simpler UserForm (minimal size with textbox and restore button) was visible and the textbox was active.
I added a little more logic to handle inserting into the cursor location of the textbox as well as straight appending. Might add some support for arrow keys in the future but I have the original functionality I set out to have.
Microsoft, if you're listening, would it have been possible to give us a RefEdit control that looks and behaves like the one built into Excel? Just a thought.