
The SendKeys method simulates keystrokes that you would manually input in the active window. Use with caution, because it can have unexpected results.
You can read more about SendKeys on the Microsoft website: SendKeys Method.
You can use the SendKeys method in your Excel macros VBA code, to simulate keystrokes that you would manually input in the active window.
The SendKeys method has two arguments: SendKeys(Keys, Wait)
For the Keys argument, you can use keys or key combinations, such as:
-- SendKeys "+{F2}" ...for Shift + F2
-- SendKeys "%ie~" ...for Alt + I, E, Enter
There is a full list of the codes, in the Keys and Key Combinations section, below
On the Comments VBA page, there are macros that use the SendKeys method to open an old-style comment (Note) for editing.
For example, the following macro inserts a comment with no user name, and opens that comment so it is ready for editing.
Sub CommentAddOrEdit()
Dim cmt As Comment
Set cmt = ActiveCell.Comment
If cmt Is Nothing Then
ActiveCell.AddComment text:=""
End If
SendKeys "+{F2}"
End Sub
In this example, SendKeys "+{F2}" is like manually pressing the Shift key, and then tapping the F2 key
That manual shortcut would open the worksheet comment in the active cell, so it is ready for you to edit the comment.

In older Excel versions, where was is a Menu Bar, the SendKeys could simulate a keyboard shortcut to run a menu command.
Sub CommentAddOrEdit()
Dim cmt As Comment
Set cmt = ActiveCell.Comment
If cmt Is Nothing Then
ActiveCell.AddComment text:=""
End If
SendKeys "%ie~"
End Sub
In this example, SendKeys "%ie~" is like the manual shortcuts
to:

If a macro uses the SendKeys method, you can run that macro from the Macro window.

Or, you could add a button to the Quick Access Toolbar, or to the Ribbon, or to a worksheet, to run the macro.

In Excel, after you create a macro, you can assign a shortcut key to run that macro.

For most macros, assigning a shortcut key is a helpful time saver.
You can quickly run a macro, without finding and clicking commands on the Excel Ribbon.
However, for some macros that use the SendKeys method, using a macro shortcut could cause a problem. The next section shows an example.
The CommentAddOrEdit, shown below, is very short, so it runs through the steps quickly.
Hoever, if I run the macro with the Ctrl+Shift+C shortcut, Excel adds a new comment, but the comment doesn't open, so I can't edit it
Sub CommentAddOrEdit()
' Keyboard Shortcut: Ctrl+Shift+C
Dim cmt As Comment
Set cmt = ActiveCell.Comment
If cmt Is Nothing Then
ActiveCell.AddComment text:=""
End If
SendKeys "+{F2}"
End Sub
In this example, SendKeys failed because:
To solve this problem, you can add a 1 second (or slightly longer) Wait time in the macro, before the SendKeys.
In the bold line of code below, I added Application.Wait, with a 1 second time.
Sub CommentAddOrEdit()
Dim cmt As Comment
Set cmt = ActiveCell.Comment
If cmt Is Nothing Then
ActiveCell.AddComment Text:=""
End If
Application.Wait (Now() + TimeValue("00:00:01"))
SendKeys "+{F2}"
End Sub
For the Keys argument, you can use keys or key combinations.
1) Key Codes
2) Combo Key Codes - (Shift, Ctrl, Alt)
These are the codes for the non-character keys on the keyboard, such as Enter, Up Arrow, or Insert.
|
Key |
Code |
|
BACKSPACE or BKSP |
{BACKSPACE} or {BS} |
|
BREAK |
{BREAK} |
|
CAPS LOCK |
{CAPSLOCK} |
|
CLEAR |
{CLEAR} |
|
DELETE or DEL |
{DELETE} or {DEL} |
|
DOWN ARROW |
{DOWN} |
|
END |
{END} |
|
ENTER (numeric keypad) |
{ENTER} |
|
ENTER |
~ (tilde) |
|
ESC |
{ESCAPE} or {ESC} |
|
HELP |
{HELP} |
|
HOME |
{HOME} |
|
INS |
{INSERT} |
|
LEFT ARROW |
{LEFT} |
|
NUM LOCK |
{NUMLOCK} |
|
PAGE DOWN |
{PGDN} |
|
PAGE UP |
{PGUP} |
|
RETURN |
{RETURN} |
|
RIGHT ARROW |
{RIGHT} |
|
SCROLL LOCK |
{SCROLLLOCK} |
|
TAB key |
{TAB} |
|
UP ARROW |
{UP} |
|
F1 through F15 |
{F1} through {F15} |
To combine keys with Ctrl, Shift and/or Alt, precede the character
with one or more of the following codes. For example:
SendKeys "+{F2}" ...for Shift + F2
SendKeys "^+{F2}" ...for Ctrl + Shift + F2
|
Key |
Code |
|
SHIFT |
+ (plus sign) |
|
CTRL |
^ (caret) |
|
ALT |
% (percent sign) |
To see the SendKeys sample code, and the list of keys, download the Excel SendKeys workbook. The zipped file is in xlsm format, and contains macros. Enable macros when you open the file, if you want to test the SendKeys macro.
Don't miss my monthly Excel newsletter! You'll get quick tips, article links, and a bit of fun. Add your email, and click Sign Up.
Next, when you get my reply, click the Confirm button. I add this step to protect you from spam!
Last updated: November 30, 2025 3:59 PM