How To Use Excel SendKeys Method In Macros - Contextures
- Topics
- Charts
- Data Entry
- Data Validation
- Filters
- Formatting
- Formulas
- Macros
- Pivot Tables
- Sample Data
- Sample Files
- Tutorial Index
| Home > Macros > SendKeys Excel VBA - SendKeys Method
|

1) SendKeys Warning
2) Using the SendKeys Method
3) SendKeys Examples
4) Run a Macro That Uses SendKeys
5) Problem Running SendKeys Macro
6) Keys and Key Combinations
7) Get the SendKeys Workbook
8) More Excel VBA Tutorials
1) SendKeys Warning
The SendKeys method simulates keystrokes that you would manually input in the active window. Use with caution, because it can have unexpected results.
- WARNING: You should only use the SendKeys Method if no other option is available, because it can cause problems, if the wrong window is active when the code runs.
You can read more about SendKeys on the Microsoft website: SendKeys Method.
2) Using the 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)
- The Keys argument is required
- This is the key or keys that you want to send to the application, as text.
- The Wait option is optional.
- Use True if Excel should wait for the keys to be processed before returning control to the macro.
- Use False (or omit this argument) if Excel should continue running the macro without waiting for the keys to be processed.
2.1) Keys Argument
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
3) SendKeys Examples
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 Sub3.1) SendKeys - Open Comment
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 the screen shot below, on the Review tab of the Excel Ribbon, the Edit Comment tooltip shows the Shift+F2 shortcut.

3.2) SendKeys - Key Combination
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 SubIn this example, SendKeys "%ie~" is like the manual shortcuts to:
- press Alt and typing I (to open the Insert menu),
- then type E (to select the Edit command),
- and then press the Enter key, to select that command.

4) Run Macro That Uses SendKeys
If a macro uses the SendKeys method, you can run that macro from the Macro window.
- On the Ribbon's View tab, click Macro
- Then click on the macro name, and click Run.

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

5) Problem Running SendKeys Macro
In Excel, after you create a macro, you can assign a shortcut key to run that macro.
- For example, in the screen shot below, the macro runs if I press the shortcut:
- Ctrl + Shift + C

5.1) Save Time With Shortcuts
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.
5.2) Conflict with SendKeys
However, for some macros that use the SendKeys method, using a macro shortcut could cause a problem. The next section shows an example.
- This problem was acknowledged on the Microsoft website:
- Error Using SendKeys in VB with Shortcut Key Assigned
- (archived article on Wayback Machine)
- Error Using SendKeys in VB with Shortcut Key Assigned
5.3) Example: SendKeys Problem
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 Sub5.4) Why SendKeys Fails
In this example, SendKeys failed because:
- The macro is only a few lines long
- I was still pressing the Ctrl + Shift keys when the macro ran the SendKeys line.
- This sent a key combination of Ctrl + Shift + F2 to Excel, instead of Shift + F2
5.5) Fix SendKeys Problem
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 Sub6) Keys and Key Combinations
For the Keys argument, you can use keys or key combinations.
- You can type a character, such as "a"
- Or, you can use codes from the 2 tables below:
1) Key Codes
2) Combo Key Codes - (Shift, Ctrl, Alt)
Key Codes
These are the codes for the non-character keys on the keyboard, such as Enter, Up Arrow, or Insert.
- Be sure to include the brace characters (curly brackets) at the start and end of the codes, such as {BACKSPACE}
| 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} |
Combo Key Codes
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) |
7) Get the Workbook
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.
Get Monthly Excel Tips!
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!
8) More VBA Tutorials
Edit Your Recorded Macro
Excel VBA Getting Started
Macro Troubleshooting Tips
FAQs, Excel VBA, Excel Macros
Create an Excel UserForm Video
UserForm with ComboBoxes
More Tutorials
Edit Your Recorded Macro
Macro Troubleshooting Tips
Last updated: November 30, 2025 3:59 PM
Từ khóa » C Vba
-
Getting Started With VBA In Office - Microsoft Docs
-
Range Object (Excel) | Microsoft Docs
-
Excel VBA - What Is VBA In Excel? Definition And Overview
-
FormulaR1C1 Property In Excel VBA (In Easy Steps)
-
What Does "c" Mean Next To A String In Vba? - Stack Overflow
-
Top 100 Useful Excel MACRO CODES Examples [VBA Library] + PDF
-
Excel VBA Tutorial For Beginners: Learn In 3 Days - Guru99
-
Visual Basic For Applications (VBA) Definition - Investopedia
-
VBA IF Statement - A Complete Guide - Excel Macro Mastery
-
VBA IF OR | How To Use IF Condition With OR Function In Excel VBA?
-
MS Excel: How To Use The MKDIR Statement (VBA) - TechOnTheNet
-
VBA Code Examples For Excel
-
Visual Basic For Applications - Wikipedia
-
Excel VBA Beginner Tutorial (Part 1 Of 3) - YouTube
