How to Remap Ctrl-Y for the VBA Editor

By | October 2, 2013

In most applications, Ctrl-Y is the keyboard shortcut for “Redo.”  (This repeats the last action, or if the last action was Undo, then it undoes the undo, which I guess is indeed a repeat of the last action!)

However, in the VBA Editor, Ctrl-Y deletes the current line of code, kind of like a more powerful version of Ctrl-X.

This is a BIG problem for VBA programmers who are very used to keyboard shortcuts.  I tried to find a workaround to this online, but after a fruitless search, decided to take a stab at this myself, and have come up with a solution.

The solution uses an AutoHotKey script, which I have provided below.  AutoHotkey is a free tool you can download and install on your Windows computer.  You will need to know how to create a text file for your script, and where to save it.  You can find a good explanation of these things here:

Turn Any Action Into a Keyboard Shortcut: A Beginner’s Guide to AutoHotkey
http://lifehacker.com/316589/turn-any-action-into-a-keyboard-shortcut

In the above article, you only need to read up through the section “How to Create a Basic Command”.  The only thing I would change about that introduction is to recommend that you use the version called AutoHotKey_L, for the reasons described here:

The Beginner’s Guide to Using an AutoHotkey Script
http://www.howtogeek.com/56481/the-beginners-guide-to-using-an-autohotkey-script/

Note that not all the lines of the script provided below are technically necessary; the lines that start with a semi-colon are just comments (which are nonetheless very useful to have!).

; = = = = = = = = = = = = = = = = = = = = = = = = =
; Map Ctrl-Y to change default behavior
; If the current window is VBA editor, perform a Redo (Alt-E, R)
; Otherwise, let the Ctrl-Y do its default behavior.
; This AutoHotKey script provided by www.kappconsulting.com


$^y::
; You want a $ at the beginning of your hotkey to prevent the endless loop
; that the ^y in your else block will otherwise trigger.
WinGetTitle, Title, A
;MsgBox, The active window is "%Title%".

if SubStr(Title, 1 , 39) = "Microsoft Visual Basic for Applications"
{
;MsgBox, "YES Microsoft Visual Basic for Applications"
MsgBox, "Next will send Alt-E, R to excecute Redo from the VBE menu."
Send !e
Sleep, 100
Send r
}
else
{
;MsgBox, SubStr(Title, 1 , 39)
;MsgBox, "Next will send Ctrl-Y to do its default behavior."
Send ^y
}

return
; = = = = = = = = = = = = = = = = = = = = = = = = =