Microsoft Office Tutorials and References
In Depth Information
Using the SendKeys Method
Now, formula-containing cells will be shaded the color you specified in step 7, constant-containing
cells will be formatted as you specified in step 10, and empty cells will have no Conditional Formatting.
Microsoft has a downloadable help file for Excel 4.0 macros at this address:;EN-US;Q128185&ID=KB;
EN-US;Q128185&FR=1 .
using THE sEndkEys METHod
The Application.SendKeys method sends keystrokes through VBA to the active application. The
term “active application” plays a key role in understanding when and when not to use SendKeys .
You’ll encounter some situations where SendKeys is the only viable alternative, and other situations
where SendKeys should not be used.
Executing a SendKeys command is a way of programmatically pressing the keys on your keyboard.
If, as a service of convenience to the users of your workbook, they need to perform an edit by
appending some text to the existing value, you can get them started with this set of SendKeys , which has the
effect of double-clicking the cell and having the cursor blinking at the end of the current value:
SendKeys “{F2}”
SendKeys “{End}”
This use of SendKeys should normally pose no problem. SendKeys gets more of a bad rap than it
should because when executed in rapid-fire succession in loops or upon inactive applications, which
is an ill-advised programming practice, the code compilation process cannot catch up with the
execution process after a time, and errors result when the intended window or object of interest is not
the proper focus. In the preceding example, the use of SendKeys is fine because it’s just one
command in a small macro for a cell that is already selected.
SendKeys is a better approach in the case of showing DataForms because the ShowDataForm
command will error if the source data’s header row starts on a row below row 2 (that is, row 3 or
below), where rows 1 and 2 are empty. The Application.SendKeys “%DO” command will call the
DataForm regardless of what row the source data starts on. Error traps and conditional statements
for data starting on row 1 or row 3 are superfluous when a simple SendKeys command can handle
the situation right then and there, whatever the first row of source data.
You may be curious about the syntax for Sendkeys . The keys for Alt, Ctrl, and
Shift are represented by the characters %, ^, and +, respectively. For example,
the expression “%c” means Alt+C; “^C” means Ctrl+C (which means copy);
and “+C” means Shift+C. The Enter key is represented by the tilde (~)
character, and keys such as Home, End, and Tab are represented by SendKeys as
{Home} , {End} , and {Tab} .
Search JabSto ::

Custom Search