VBA Excel - Replace Enter key with Alt+ Enter - excel

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?

Related

VBA - Closing or clicking OK in MsgBox from another workbook,

Hi I the following code in Excel VBA,
Sub A ()
Workbooks.open ("A.xls")
ActiveWorkbook.Worksheets("1").Select
ActiveSheet.CommandButton1.value = true
End Sub
I'm opening another workbook (the code inside is protected so I can't modify the workbook "B") and clicking a run button on the worksheet, and it returns a MsgBox with OK button.
I want to know how I can use VBA to close this MsgBox or clicking "OK" ...I tried to use,
`application.DisplayAlert = false`
before opening the workbook "B" but this does not work..
Thanks for your help!
There might be an approach using SendKeys -- but SendKeys is notoriously temperamental. Here is a more reliable approach:
1) If you don't already have it organized like this, have the click-event handler of the button a 1-line sub that looks like:
Private Sub CommandButton1_Click()
Process
End Sub
Where Process is the sub which actually does the heavy lifting. In general, I think it a good idea to have event handlers mostly functioning as dispatchers to subs.
2) Change the code for Process (or whatever you choose to name it) in the following way:
a) Modify the first line to look like
Sub Process(Optional Verbose As Boolean = True)
b) Where Process has something like
MsgBox "Processed!"
replace it by
If Verbose Then MsgBox "Processed!"
3) In the code you gave above, replace the line
ActiveSheet.CommandButton1.value = true
by the line
Application.Run "A.xls!Process", False
This approach will bypass the button entirely and run the code which the button normally triggers, but run it in silent mode.
On Edit: To use SendKeys you could do the following. Put the line
Application.SendKeys "~"
before the line
ActiveSheet.CommandButton1.value = True
~ is the character shortcut for Enter. SendKeys doesn't itself send the keystroke, instead it puts something on the Windows Message Queue. VBA doesn't have any direct control about exactly when this message will be processed. In this case the lack of control is a benefit. The VBA interpreter moves onto the next line, which triggers the MsgBox. By the time the SendKeys message is processed the default Okay button on the message box has the focus hence receives the enter key. This can even happen before the box is painted, making it seem that the MsgBox was never there -- but that is better to think of it as being destroyed before you have time to see it.
The reason why it is necessary to have the SendKeys line before the line which clicks the button is that once the message box appears it will cause the VBA interpreter to wait until it is closed -- hence the calling code will suspend its execution until after the message box is closed, hence the SendKeys wouldn't be processed until it is no longer needed.
I don't really trust SendKeys. I suspect that sometimes when you run the code what will happen is that A1 in the newly activated sheet will receive the enter key (shifting the selection from A1 to A2) before the message box appears. I'm not sure if this can happen, but if it does a workaround might be to move the SendKeys to a VBScript program. Launch this program (with window minimized and not waiting for return) before the button is clicked. The VBScript program can have say an 0.5 second pause before it uses SendKeys. The script will be running in a different thread so it won't be blocked by the message box.

Calling subroutine when "ENTER" is pressed

I am new to VBA. I have made a look-up function using vba and it works so fine. Here is what I wanted, I want to call my subroutine (that contains the look-up) when the Enter key is pressed. How am I going to do this? How does an event like this in VBA work?
Use Application.OnKey with a Workbook_Open event. This way, every time your workbook is loaded, you can call your macro on pressing Enter. See below:
Private Sub Worksheet_Open(ByVal Target As Range)
Application.OnKey "{RETURN}", "MyLookUp"
End Sub
Make sure you paste it in the ThisWorkbook's code. Let us know if this helps.
workbook> in the Open (and window activate) Subroutine :
Application.OnKey "{RETURN}", "Sub_Enter"
Application.OnKey "{ENTER}", "Sub_Enter"
the is a difference between numpad Enter and the other Enter.
and on workbook close (and change_window...) : (to deactivate it)
Application.OnKey "~"
Application.OnKey "{ENTER}"
You will also need a custom bit of code to know if the content of a cell has changed + ENTER. (in that case your macro Enter will have to ignore and exit sub)...

Excel combo box - How do you use the tab key to select a cell?

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

Excel – Detect if 2 specific keys being pressed at once

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

Entering a key stroke 'down arrow' into an excel formula

I have an excel concatenate formula that I need to paste into another program. I have successfully entered the tab keystroke with CHAR(9), but I can't seem to get the down arrow keystroke to work. I have tried using CHAR(40), but the program I am pasting to does not recognize this.
Here is my formula:
=CONCATENATE(M16,CHAR(9),CHAR(9),CHAR(9),CHAR(9),CHAR(9),M17,CHAR(9),CHAR(9),
CHAR(9),CHAR(9),CHAR(9),LEFT(M9,55),CHAR(9),CHAR(9),CHAR(9),CHAR(9),CHAR(9),
RIGHT(M9,100),CHAR(9),CHAR(9),CHAR(9),CHAR(9),CHAR(9),"=",M18,CHAR(9))
I want to replace the 5x CHAR(9), which is 5x the tab key, with just one text entry for down arrow.
Does anyone know another way of doing this other than CHAR(40)?
I'm not entirely clear about what you're after, so I'll try to cover both bases:
If you want the text to start on t new line then:
char(13) will give you a carriage return
char(10) will give you a line feed
If you are (literally) sending these keystrokes to another application then you may also want to investigate the Application.SendKeys method which sends keystrokes to the active application. You can do things like:
Sub MySub()
AppActivate ("Notepad")
SendKeys ("Some text{TAB}{DOWN}{TAB}Some more text")
End Sub

Resources