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.
Syntax
Below is the syntax of the VBA SendKeys method.
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.
Other keys have different keys and characters. The below table shows the detailed explanation for each key.
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
Example #1
For example, look at the below cell value.
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
First, we need to select the comment cell to edit the comment. So use the code RANGE(“A1”).Select
4.9 (1,353 ratings) 35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
Code:
Sub Send_Keys_Example() Range("A1").Select End Sub
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”.
If you press this key, it will be editing the comment.
Now open the “SendKeys” method.
In the SendKeys method, the character for using the SHIFT key is “+” (Plus sign), so enter the “+” sign-in code.
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
Now execute the code and see what we get.
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.”
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.
You can see that the Edit comment option is enabled.
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
Choose the macro that you need to run and then click on Run.
When you run the code, it will open up below paste special dialogue box.
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 –
- 35+ Courses
- 120+ Hours
- Full Lifetime Access
- Certificate of Completion