VBA SendKeys

Last Updated :

21 Aug, 2024

Blog Author :

Edited by :

Ashish Kumar Srivastav

Reviewed by :

Dheeraj Vaidya, CFA, FRM

Table Of Contents

arrow

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

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.

Examples to use Excel VBA SendKeys Method

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: -