Sendkeys
Sendkeys
html)
SendKeys Warning
Keys Argument
SendKeys Examples
SendKeys Warning
The SendKeys method simulates keystrokes that you would manually input in the active
window. Use with caution, because it can have unexpected results.
WARNING: You should only use the SendKeys Method if no other option is
available, because it can cause problems, if the wrong window is active when the
code runs.
You can read more about SendKeys on the Microsoft website: SendKeys Method
(https://msdn.microsoft.com/en-us/library/office/aa202943(v=office.10).aspx).
• The Keys argument is required, and is the key or keys that you want to send to the
application, as text.
• The Wait option is optional.
◦ Use True if Excel should wait for the keys to be processed before returning
control to the macro.
◦ Use False (or omit this argument) if Excel should continue running the macro
without waiting for the keys to be processed.
Keys Argument
For the Keys argument, you can use keys or key combinations, such as:
There is a full list of the codes at the end of this page: Keys and Key Combinations
SendKeys Examples
On the Comments VBA (https://www.contextures.com/xlcomments03.html) page, there are
macros that use the SendKeys method to open a comment for editing. For example, the
following macro inserts a comment with no user name, and opens that comment so it is ready
for editing.
Sub CommentAddOrEdit()
In this example, SendKeys "+{F2}" is like pressing Shift and tapping the F2 key, to edit the
comment in the active cell.
In Excel 2003, and earlier versions, where there is a Menu Bar, the SendKeys could simulate a
keyboard shortcut to run a menu command.
Sub CommentAddOrEdit()
Or, you could add a button to the Quick Access Toolbar (https://www.contextures.com/excel-
quick-access-toolbar-customize.html), or to the Ribbon
(https://www.contextures.com/excelribbonaddcustomtab.html), or to a worksheet
(https://www.contextures.com/excel-vba-worksheet-macro-buttons.html), to run the macro.
Problem Running SendKeys Macro
If the SendKeys command isn't working correctly, one of these solutions might help fix the
problem
In this example, the shortcut Ctrl + Shift + C has been assigned to the CommentAddOrEdit
macro.
After creating the shortcut, if you press Ctrl + Shift + C, a blank comment is inserted in the
cell, but the comment does not open for editing -- the SendKeys method doesn't work.
The problem occurs because this is a very short macro, and you are still press the Ctrl + Shift
keys when the macro runs the SendKeys statement. This sends a key combination of Ctrl +
Shift + F2 to Excel, instead of Shift + F2.
To solve the problem, you can add a 1 second (or slightly longer) Wait line in the macro,
before the SendKeys.
Sub CommentAddOrEdit()
Dim cmt As Comment
Set cmt = ActiveCell.Comment
If cmt Is Nothing Then
ActiveCell.AddComment Text:=""
End If
Application.Wait (Now() + TimeValue("00:00:01"))
SendKeys "+{F2}"
End Sub
Key Code
BREAK {BREAK}
CLEAR {CLEAR}
END {END}
ENTER ~ (tilde)
HELP {HELP}
HOME {HOME}
INS {INSERT}
RETURN {RETURN}
TAB {TAB}
UP ARROW {UP}
To combine keys with Ctrl, Shift and/or Alt, precede the character with the following codes.
For example:
SendKeys "+{F2}" ...for Shift + F2
Key Code
CTRL ^ (caret)
Related Pages
Edit Your Recorded Macro (https://contexturesblog.com/archives/2009/11/27/excel-vba-
edit-your-recorded-macro/)
More Tutorials
Edit Your Recorded Macro (https://contexturesblog.com/archives/2009/11/27/excel-
vba-edit-your-recorded-macro/)
(https://www.contextures.com/signup01)
(https://www.contextures.com/pivotpowerfreeaddin.html)
(https://www.contextures.com/exceltoolsaddin.html)
(https://www.contextures.com/peltiertech)
(https://www.contextures.com/datavalidationmultiselectpremium.html)
(https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612)Debra Dalgleish
(https://mvp.microsoft.com/en-us/mvp/Debra%20%20Dalgleish-7612)