Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Dec 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Use Keyboard shortcut in macro?

    I had a really annoying problem, which I've kind of solved, but was wondering if it could be done more elegantly or simply. since it is about as non-intuitive as you can get at the moment. What I was trying to do was to emulate the pressing of one of the buttons on the ribbon in Excel 2010 from a macro. Eventually I ended up with this

    Const cIncreaseDecimal = 398
    Application.CommandBars.FindControl(ID:=cIncreaseD ecimal).Execute

    which is from "old" Excel (pre 2003), but works in 2010. However, while researching this I thought it would be much easier and wouldn't require me finding the "ID" of the command, if I could call a keyboard shortcut from within a macro. Then I could assign whatever shortcut I wanted to whatever button I wanted, then just call that KB shortcut from in the macro. I know the opposite is possible (ie calling a macro from a KB shortcut), but is it possible to do this?

    I initially tried recording a macro of the button press and using that ,which usually works fine, but the code for the button seems to read the current number of decimals and then it just runs this when I increased the number of decimals, so some of the code is actually missing!!

    Selection.NumberFormat = "0.0000" (my original number had 3 decimal places).

    Anyway, if anyone knows a way to run a keyboard shortcut in a macro, I'd really like to know.

    Thanks!



  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    5,895
    Thanks
    0
    Thanked 85 Times in 81 Posts
    Using the commandbars is better than Sendkeys which is what you would need to simulate a keyboard shortcut. (for 2010 you can use application.CommandBars.ExecuteMso "DecimalsIncrease")
    Regards,
    Rory
    Microsoft MVP - Excel.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •