FLASH SALE! - "CHATGPT AND ARTIFICIAL INTELLIGENCE FOR MICROSOFT EXCEL AT 60% OFF" Enroll Now

VBA SendKeys

Updated on June 5, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

Excel VBA SendKeys

SendKeys in VBA language is a method to send keystrokes to the active window so we can work manually after that. Whenever we use alphabets as the keys, all the alphabets need to be in lowercase characters. It is a complex method and recommended to use only if necessary and when you are out of options.

“SendKeys” is one of the more complex topics to understand. Not many of us use this feature in VBA, but it is always good to have more knowledge on more topics. This article will show you how to use the SendKeys function. You may find it difficult to reread the article multiple times with a practical approach to learn fast and better.

SendKeys-in-VBA

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA SendKeys (wallstreetmojo.com)

Syntax

Below is the syntax of the VBA SendKeys method.

VBA SendKeys Syntax

Keys or String: The kind of key that we need to send to the active application.

Wait: In this argument, we can use two things: TRUE or FALSE.

  • TRUE if you want the Excel to wait for the assigned Keys to process before returning the control to the Macro.
  • FALSE if you ignore the Wait parameter, this will be the default value. If you choose FALSE, Excel continues to run the Macro without waiting for the keys to process to the active window.

We use the keyboard’s common keys: “Ctrl, Shift, and ALT.” So, with the SendKeys method, we need to use them with special characters. The below table shows the special characters for the above three common keys.

KeyCharacterCharacter Description
Ctrl^Caret
Shift+Plus Sign
ALT%Percent Sign

Other keys have different keys and characters. The below table shows the detailed explanation for each key.

KeyCode
BACKSPACE{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}
ESCAPE{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{TAB}
UP ARROW{UP}
F1 through F15{F1} through {F15}

As per the requirement, we can use any of the above keys. With some practical examples, we will show you how to use SendKeys.

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

Examples to use Excel VBA SendKeys Method

You can download this VBA SendKeys Excel Template here – VBA SendKeys Excel Template

Example #1

Look at the below cell value.

vba sendkeys Example 1

We have values in three cells. For example, in the first cell, we have a value of “Bangalore.” So for this cell, there is a comment as the “Capital City of Karnataka.”

Now, using “SendKeys,” we try to edit this comment.

Open the Excel sheet. Go to the Visual Basic Editor, and start the VBA subprocedure.

Code:

Sub Send_Keys_Example()

End Sub
vba sendkeys Example 1-1

First, we need to select the comment cell to edit the comment. So, use the code RANGE(“A1”).Select

Code:

Sub Send_Keys_Example()

Range("A1").Select

End Sub
vba sendkeys Example 1-2

Once the cell is selected, we will act on editing the comments. Here, we must recollect the keyboard shortcut we used to edit the comment.

We use the shortcut key “Shift + F2” to edit the comment.

Edit Comment Shortcut Key

If you press this key, it will edit the comment.

Now, open the “SendKeys” method.

vba sendkeys Example 1-3

In the SendKeys method, the character for using the SHIFT key is “+” (Plus sign), so enter the “+” sign-in code.

vba sendkeys Example 1-4

Now, plus sign works as a SHIFT key. The next key, along with SHIFT, we use is the F2 key. We need to enclose function keys with curly brackets whenever we use function keys. So, enter the function key F2 in the curly bracket.

Code:

Sub Send_Keys_Example()

Range("A1").Select

SendKeys "+{F2}"

End Sub
vba sendkeys Example 1-5

Now execute the code and see what we get.

Error Example 1-6

When we tried to execute the code, we got the message above. One of the key things we need to keep in mind is we cannot run the Macro, which uses “SendKeys” from the Visual Basic Editor window.

We need to run the code from the “Macro” list.

Close the Visual Basic Editor Window first.

Go to the “Developer” tab and click on “Macros.”

Macros Example 1-7

Now, a list of all the Macros opens up. Choose the Macro that you need to run. Our Macro name is “Send_Keys_Example,” so we will press the “Run” button.

Comment cell Example 1-8

You can see that the “Edit” comment option is enabled.

vba sendkeys Example 1-9

As you can see above, it has assigned the shortcut key of SHIFT + F2 to open the edit comment option.

Example #2

We can do this if you want to open the “Paste Special” window through the SendKeys method. But, first, we need to copy certain cells and then use the SendKeys.

Code:

Sub Send_Keys_Example1()

Range("A1").Copy

SendKeys "%es"

End Sub
Paste special code Example 2

Choose the Macro you need to run and click on “Run.”

Paste special Example 2-1

When you run the code, it will open below the Paste Special dialog box.

vba sendkeys Example 2-2

Things to Remember

  • The SendKeys assigns keystrokes to the active application.
  • This method is complex and recommended only if necessary and when you are out of options.
  • Whenever we use alphabets as the keys, all the alphabets need to be in lowercase characters.

This article has been a guide to SendKeys in VBA. Here, we discuss the examples of the VBA SendKeys method,  used to send keystrokes to the active window to work manually in Excel. Below you can find some useful Excel VBA articles: –