SendKeys Method [Excel 2007 Developer Reference]

Switch View :
ScriptFree
Application.SendKeys Method
Sends keystrokes to the active application.

Syntax

expression.SendKeys(Keys, Wait)

expression   A variable that represents an Application object.

Parameters

NameRequired/OptionalData TypeDescription
KeysRequiredVariantThe key or key combination you want to send to the application, as text.
WaitOptionalVariantTrue to have Microsoft Excel wait for the keys to be processed before returning control to the macro. False (or omitted) to continue running the macro without waiting for the keys to be processed.

Remarks

This method places keystrokes in a key buffer. In some cases, you must call this method before you call the method that will use the keystrokes. For example, to send a password to a dialog box, you must call the SendKeys method before you display the dialog box.

The Keys argument can specify any single key or any key combined with ALT, CTRL, or SHIFT (or any combination of those keys). Each key is represented by one or more characters, such as "a" for the character a, or "{ENTER}" for the ENTER key.

To specify characters that aren't displayed when you press the corresponding key (for example, ENTER or TAB), use the codes listed in the following table. Each code in the table represents one key on the keyboard.

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)
ESC{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}

You can also specify keys combined with SHIFT and/or CTRL and/or ALT. To specify a key combined with another key or keys, use the following table.

To combine a key withPrecede the key code with
SHIFT+ (plus sign)
CTRL^ (caret)
ALT% (percent sign)

Example

This example uses the SendKeys method to quit Microsoft Excel.

Visual Basic for Applications
Application.SendKeys("%fx")



Community Content

cd112358
Dealing with Windows
let's say you have A1: yes, A2: no, A3: maybe, A4 and on are blank

Range("A1").Activate
SendKeys("^{DOWN}")
DoEvents 'do this to be sure the queue is processed
Range("B1") = Activecell.text

'B1: maybe, if you don't use DoEvents, the answer in B1: yes
'either way, at the end of the macro you see the active cell at A3 so you get confused.

Important note:  if you use debugger to step though the lines of code, DoEvents won't execute like runtime.  I think it must have something to do with the event processing in debug mode and it makes sense to me eventhough I don't know the exact mechanism behind the debugger.  Anyway, you can still debug this code but use a break point right after the DoEvents, run the code and let it'll stop right after the sendkeys is process

Aditya Mandlekar
SendKeys Method doesnt work for an Excel Application
My following code to hit enter for a popup message generated is not working,Please suggest if you can help me understanding the Problem

Dim WScript,WshShell
                set WshShell = CreateObject("WScript.Shell")
               
'                WshShell.Run "EXCEL.EXE", 1

                Set xl =CreateObject("Excel.Application")
                 
                 
            
                Set objWorkbook=xl.Workbooks.Open("C:\Profiles\mkt0x5\Desktop\test_macro.xls")
                xl.Visible = True
                   
            
                xl.Workbooks.OpenXML FilePath , , xlXmlLoadImportToList
           
                  xl.sendKeys "~"