Thread: Use Keyboard shortcut in macro?
2012-06-13, 08:50 #1
- Join Date
- Dec 2005
- 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
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.
Subscribe to our Windows Secrets Newsletter - It's Free!
Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!
+ Get this BONUS — free!
Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!
2012-06-13, 09:31 #2
- Join Date
- Dec 2000
- Burwash, East Sussex, United Kingdom
- Thanked 80 Times in 76 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,
Microsoft MVP - Excel.