WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Blog
  • Free Video Tutorials
  • Courses
  • All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA SendKeys

VBA SendKeys

By Jeevan A YJeevan A Y | Reviewed By Dheeraj VaidyaDheeraj Vaidya, CFA, FRM

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

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.

VBA Sendkeys special characters

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

VBA Sendkeys other keys.png

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

Popular Course in this category
Sale
All in One Excel VBA Bundle (35 Courses with Projects)
4.9 (1,353 ratings)
35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
View Course

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 Editor 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 Special” 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.

Recommended Articles

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 –

  • VBA ReDim Array
  • VBA ByVal
  • VBA String Comparison
  • VBA On Error GoTo
0 Shares
Share
Tweet
Share
All in One Excel VBA Bundle (35 Courses with Projects)
  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Download Coursera IPO Financial Model

By continuing above step, you agree to our Terms of Use and Privacy Policy.
WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA SendKeys Excel Template

Coursera IPO Financial Model & Valuation Free Download