Calling Excel shortcuts from VBA

  • Thread starter musicgold via OfficeKB.com
  • Start date
M

musicgold via OfficeKB.com

Hi,

I am trying to automate the following two excel shortcuts using VBA. I know I
can use commandbar and commandcontrol to call excel menus but I can not find
the submenus these shortcuts belong to.

1. Ctrl + [ -- cursor goes to the precedent cell

2. F5 + Enter -- cursor goes to the original cell


Also, is it possible to call an excel shortcut from VBA? I know that one can
call a worksheet function from VBA (For example, Application.
WorksheetFunction.Fact(5) ). But is there way to call a shortcut, say Ctrl +
C from VBA?

Thanks,

MG.
 
J

JLGWhiz

VBA is pretty straight forward in moving the cursor to cells.

Range("C5").Select

The above VBA command puts the cursor in cell C5. You can also use the GoTo
method to move the cursor to specific cells.

As for the precedent and original cells. You have to define these cells in
code with a variable.

myPcell = ActiveCell
myPcell.Select

myPcell variable will hold the address of that ActiveCell, no matter which
other cells become the active cell thereafter. So when you select myPcell,
it will return to that original active cell.

Shortcut keys are usually assigned to functions and procedures (macros) that
a user needs to call periodically to perform a specific action in a
worksheet. These procedures have a title line with a name that appears in
the dialog box which can be displayed by clicking Tools> Macro> Macros in
Excel. There is more than one way to call them from VBA.

1. Just the Name
macro1
This can be used if there is no argumentr to be passed to the
procedure.

2. Using Call
Call macro1
When Call is used when passing arguments to be enclosed in parentheses.
If you use
the key word Call, then you must use the parentheses, even if there are
no arguments.

3. Using Run
Application.Run("<path>!macro1", <arg>)
This method allows you to run a macro from another workbook.

These are all explained in more detail in the VBA help files. Just type in
keyword Call or Run to view the topic list, then select the topic from the
list.
 
M

musicgold via OfficeKB.com

JLGWhiz,

Thanks. I know basic fundamentals related to macros in Excel.

The shortcuts I mentioned in my original message are Excel shorcuts. I am not
able to find the functions underlying them. I want to be able to execute
those shortcuts inside a macro.

MG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top