How to type on the keyboard in VBA?

You can use VBA to automatically “type” characters (and numbers) on the keyboard. In addition, it can also perform keyboard actions, such as Control+C (copy), Control+A (select all), Alt+Tab, etc. (See examples below) Almost anything that can be done on the keyboard is possible through VBA. This is very useful as you can automate many repetitive tasks in Excel.

In this practical example, VBA sends the keystrokes into the currently active window (which does not necessarily need to be Excel). However, this may cause problems if somehow an incorrect window was activated.

Sub TypeInCurrentWindow()
SendKeys "^a", True 'Control+A Select All
SendKeys "^c", True 'Control+C Copy
SendKeys "abcd", True 'Types abcd into active window
End Sub

The basic statement to get VBA to “type” characters is:

SendKeys "characters to be typed", Wait (Optional)

Note that the “Wait” is an optional parameter. If False (default), the statement returns immediately after keystrokes are sent. If True, the statement returns after keystrokes are processed.

Delay Commands For Slower Applications

Occasionally, you may be running into problems where your keystrokes are not entirely recognized, and some of your keystroke commands are skipped or not showing as intended. It may not a bug in your script. Instead, it may be because the application you are typing in needs extra time to process the keystrokes being sent. As a result, you may need the VBA script to take a small break between each SendKey command and allow the application to catch up. We can do that with a Sleep command.

Sub TypeInCurrentWindow()
SendKeys "^a", True 'Select All
Sleep 400 'Wait 0.4 seconds (400 milliseconds)
SendKeys "^c", True 'Copy
Sleep 400 'Wait 0.4 seconds
SendKeys "abcd", True 'Type abcd
Sleep 400 'Wait 0.4 seconds
End Sub

Note that Sleep makes the computer wait in the # of milliseconds.

Special Characters

Shift is +
Control is ^
Alt is %

For full list of special characters, please refer to this link. http://msdn.microsoft.com/en-us/library/aa266279(v=vs.60).aspx

Private Sub SpecialCharExample()
SendKeys "^ac", True '1. Press control+a, then character "c"
SendKeys "^(ac)", True '2. Press control+a+c (continue to hold control) 
End Sub

In statement 1, VBA selects all, then press “c”. In statement 2, VBA selects all then copied everything that was selected.

You can get VBA to switch windows by sending Alt+Tab keystrokes too. This is useful to automate a script on various windows or a window outside of Excel. (However, note that this does not work in Windows 8.)

Private Sub SwitchWindows()
SendKeys "%{TAB}", True 'Alt+Tab (Switching windows)
End Sub

Escape Characters

If you are actually trying to type one of the special characters literally, you can escape them by enclosing them in brackets {}.

Private Sub EscapeExample()
SendKeys "{+}{^}", True 'Types "+^"
End Sub

One thought on “How to type on the keyboard in VBA?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>