I don’t know if this is possible. I am working with Excel and using VB script. Is there a way to detect if two keys are being pressed at the same time and which ones they are? Then if it is those keys that are pressed I can use an If/Then statement to do whatever processes I need to do?
I know if I have something like a combo box I can use the keydown feature to detect a single key pressed, but this will only work on one key and not two keys. Also, I am not using combo boxes, text boxes, or anything else. I am strictly using cells, so there doesn’t appear to be anything like keydown to detect even the press of a single key.
Again, I would need for it to detect two keys at the same time being pressed. I would also somehow like to get it to detect this at the workbook level as well, instead of each individual worksheet, since there are several worksheets and would like these pressed keys to work from one sheet to the next.
Please let me know if this is possible or not, but I have a feeling it is not possible.
Doug,
Thanks for your suggestion, I figured everything out due to that. Here it is, in case someone else finds this useful:
Private Sub Workbook_Activate()
'When the workbook is active this will run the script in the place of the standard Ctrl + C.
Application.onkey "^{c}", "ThisWorkbook.cCopy"
End Sub
Private Sub Workbook_Deactivate()
'When another workbook is active this will disable this script so the standard Ctrl + C will work again.
Application.onkey "^{c}"
End Sub
Sub cCopy()
'This is the script to run when active. This was used for testing purposes only.
Worksheets("Sites").Range("I1").Value2 = "Yes"
End Sub
Related
I am trying to replace Enter key with Alt+Enter so that I can write multiline in cells with ease.
I have seen that there is a function Application.OnKey and Application.SendKeys and I wanted to use those something like this:
Application.OnKey "~" , Application.SendKeys("%~")
But where do I place those? Or any other ideas?
I think I agree with #Andreas, this is unlikely to work using these methods.
This is what I tried: I made a button Button1 and in its click method I assign the Enter key to send Alt-Enter as you suggest in the question:
Sub Button1_onClick()
Call Application.OnKey("~", "SendAltEnter")
End Sub
Sub SendAltEnter()
Application.SendKeys ("%~")
End Sub
This does in fact re-route the Enter key, but apparently the Alt-Enter results in another call to the method for the "Enter" part of "Alt-Enter" -- it results in an infinite loop the first time you hit enter after having clicked the button, and you have to restart your Excel application to clean it up.
I also tried this, simply using another key near Enter, namely # (at least on German keyboards) which you could hit instead of Alt-Enter:
Sub Button1_onClick()
Call Application.OnKey("#", "SendAltEnter")
End Sub
Sub SendAltEnter()
Application.SendKeys ("%~")
End Sub
The key '#' is intercepted, but not if you are in input mode in a cell, only if the focus is somewhere in the worksheet.
I think you'll have to do this outside of Excel using a keyboard remapping tool for Windows. I quickly found https://www.howtogeek.com/710290/how-to-remap-any-key-or-shortcut-on-windows-10/ by googling but know nothing about it or if it is legit or not.
Have you considered just using Shift + Enter to insert a carriage return instead?
I want, when I press the return key, to move the selection one cell to the right.
I know this option can be changed under options in Excel.
I want to set this option programmatically so it only works with one workbook when it loads, but not with other workbooks.
So the option will be reset back to the original "down" action when the workbook is closed.
While maybe not completely recommended – using TAB would be how most people work this – You can change the option with the MoveAfterReturnDirection property.
So if you would have something like
Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
Application.MoveAfterReturnDirection = xlToRight
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
Application.MoveAfterReturnDirection = xlDown
End Sub
in the code for the workbook, that should accomplish something along the lines of what you are asking.
Notice that this will change the setting for anyone who are using the workbook, and if they had anything else than Down set, then that will change.
You could work around this by capturing the original value using a global variable, but I wouldn't bet my life on that it wouldn't mess up someones setting at some point.
Actually, testing the latter managed to somehow set my settings to "left", after closing the book.
An even worse way to do it would be to completely change the function of the Enter key, using the OnKey method. Making the Enter key return a Tab key press. The approach would be the same, but using OnKey and then creating a sub that uses something like SendKeys to return the Tab press.
I have a chart that I want user not to delete. Yet I'd like to give him option to move or resize it if he wants to, so I think, that option locked in properties is not appropriate here (I use secure worksheet too).
Is there any possibility here to make code that works when user clicks Del on the keyboard, then some msgBox with information with error, that this chart is not deletable appears and then discard this action of removing?
It is impossible to prevent Chart deletion, while simultaneously enabling it to be moved
There are two following options, which *sorta* achieve what you want:
Enable Sheet Protection (Review -> Protect Sheet) which will achieve the following:
prevent the Chart from being deleted
unfortunately also prevent chart from being moved
Use Application.OnKey to detect press of Delete
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "PreventDeletion"
End Sub
Private Sub PreventDeletion()
MsgBox ("Deletion not allowed in this worksheet")
End Sub
this allows the chart to be moved
unfortunately only creates an illusion of delete protection. If user uses mouse2 and selects the delete option, it will still be available to him. This only interrupts pressing the Delete
also this renders Delete obsolete for entire Sheet, which can be annoying (for the users)
Neither option is ideal, though personally I prefer the first one, as preventing usage of Delete altogether is bound to annoy your users.
I am using a combo box in Excel. Is there a way that I can use the tab key to select a specific cell when I am in the combo box? For example if I just selected something from the dropdown list or typed it in, can I somehow press the tab key to select the next field that would need to be filled in? This is the only object on the spreadsheet, so the next item I would want to select is a cell. When I press the tab key I can’t get it to do anything.
This tidbit of information probably will not be of any use, but I am using VB script to populate the combo box. The script works great so I don’t need help with that. The tab thing will not work if it is a new combo box without script either. I thought I would mention this because I would be willing to use some sort of script in order to get the tab key to work.
I am using Office 2010 and Windows 7, if this information is needed.
Thanks for any help someone can provide.
Chris
I figured out how to get this to work! Here it is, in case someone else should find this useful. My combo box is called cboSites. You can also add additional If statement under the KeyDown sub to get it to do different stuff. Like if you want to use the enter key instead do a or statement with the value of 13 instead of 9. You can do any key on the keyboard this way.
Private Sub cboSites_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Then
Range("B5").Select
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("B4"), Target) Is Nothing Then
ActiveSheet.cboSites.Activate
End If
End Sub
I am at a dead end, i have been trying to hide all macros, I have been able to do all macros that do NOT have a short Key assigned by Private Sub, but whenever i use Private on a macro that has a shortcut key assigned it doesn't not work, in fact, it disables the shortcut key
I have tried
Private Sub Workbook_Open()
Application.OnKey "+Q", "Macro1"
End Sub
and that doesn't work, yes I placed it in This Workbook.....of course if i take Private out of the macro and run it works fine. I have tried ^+Q and that doesn't work either
there has to be a way, isn't there?
Two things:
First, if you need capital "Q" then you need to do:
Application.OnKey "^+Q", "Macro1"
Also, it isn't necessary to make the macros private in order to hide them from the ribbon/macros menu. Here is a workaround that I use:
Any macro/subroutine will be 'hidden' (not displayed in the list of macros from the Ribbon/menu) if it takes at least one argument. So one workaround for you is to just add an optional, meaningless argument for each subroutine, and then you can leave them as public subs.
Example:
Sub Macro1(Optional dummy)
MsgBox "Hi!"
End Sub
The above macro should not appear in the list of available macros, but it should still work with your hotkey.