VBA SendKeys

Excel VBA SendKeys

SendKeys in VBA language is a method used to send keystrokes to the active window so that 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 complex topics to understand. Not many of us use this feature in VBA, but it is always a good thing to have more knowledge on more topics. In this article, we 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 which we need to send to the active application.

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

  • TRUE if you want the excel to wait for the assigned Keys to be processed before having the control back to the macro.
  • FALSE, if you ignore the Wait parameter, this will be the default value. If you choose FALSE, then excel continues to run the macro without waiting for the keys to be processed to the active window.

The common keys we use with the keyboard are “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 the way of using the SendKeys.

Examples to use Excel VBA SendKeys Method

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

Example #1

For example, look at the below cell value.

vba sendkeys Example 1

We have values in three cells, and in the first cell, we have a value of “Bangalore,” and 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 and go to the visual basic editor, start the VBA subprocedureVBA SubprocedureSUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more.

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 select, we will perform the action of editing the comments. Here we need to recollect the keyboard shortcut we use to edit the comment.

In order to edit the comment, we use the shortcut key “Shift + F2”.

Edit Comment Shortcut Key

If you press this key, it will be editing 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. Whenever we use function keys, we need to enclose them with curly brackets, 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 try to execute the code, we got the message as 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 Visual Basic EditorVisual Basic EditorThe Visual Basic for Applications Editor is a scripting interface. These scripts are primarily responsible for the creation and execution of macros in Microsoft software.read more Window first.

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

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 I will hit on 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

For example, if you want to open the “Paste SpecialPaste SpecialPaste special in Excel allows you to paste partial aspects of the data copied. There are several ways to paste special in Excel, including right-clicking on the target cell and selecting paste special, or using a shortcut such as CTRL+ALT+V or ALT+E+S.read more” window through the SendKeys method, we can do this as well. 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 that you need to run and then click on Run.

Paste special Example 2-1

When you run the code, it will open up below paste special dialogue box.

vba sendkeys Example 2-2

Things to Remember

  • SendKeys assigns keystrokes to the active application.
  • This method is so complex and recommends to use 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 has been a guide to SendKeys in VBA. Here we discuss the examples of the VBA SendKeys method, which is used to send keystrokes to the active window to work manually in excel. Below you can find some useful excel VBA articles –

  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>