New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Function Keys

Macros, VBA, Excel Automation, etc.

Function Keys

Postby jetjockey » Sat Jan 14, 2012 2:38 pm

Hi,

I am porting my workbook to a tablet and I need to be able to remap the Function Keys to run my macros!

IE F1 will call up sheet 1 instead of the MS help
F2 will call Sheet 2
Ctrl+F1 will call sheet 3
Ctrl+F2 will call sheet 4
Ctrl+F4 will call sheet 5
Ctrl+F5 will call sheet 6
Shift+Ctrl+F1 call sheet 7
and so on in all I am remapping F1, F2, F3, Ctrl+F1 to Ctrl+F6, and Shift+Ctrl+F1 to Shift+Ctrl+F6

While Ctrl+F12 will call Print via a macro

I have heard of:

Application.Onkey "{F1}", "mymacro"

Then

Sub mymacro ()
Application.SendKeys " "

But I can't make it work!

Any word of wisdom please lots of details...

You can see what I am working on at www.globers.com // basically I am linking the macro buttons to the F keys

Thanks Pierre
  • 0

jetjockey
Rookie
 
Posts: 12
Joined: Jan 14, 2012
Location: Trenton On
Reputation: 0

Re: Function Keys

Postby Sisyphus » Sat Jan 14, 2012 3:15 pm

Hi,
Start with this procedure:
Code: Select all
Private Sub Workbook_Open()
   Msgbox "Workbook is opening"
End Sub

Try it out. Every time you open the workbook you should see this message. If you don't you have done something wrong.
Now take out the code for the message box and insert this code instead:
Code: Select all
Application.OnKey "{F1}", "ShowSheet1"

You know that this code will run when you open the workbook because you tested your Open Event procedure. But you can't run it because you don't have the procedure ShowSheet1 yet. So, insert an apostrophe before the line so that you can still open and close your workbook without getting error messages. Remove the apostrophe when your procedure is ready.

You will have to make a separate procedure for each key you want to assign a function to, and a separate OnKey call for it in the same Open event procedure. So, you now settle down to write the procedure to show Sheet 1. I can't help you with that because I don't understand your problem. My tablet is an iPad. It has no Function keys and I can't load Excel workbooks on it.

But I would like to add one word of caution:
Once you assign another function to F1 the original function is lost and you should think of how to get it back. The way to do it is to run another event procedure when you close the workbook. It would be nice if you would have a command that you can run there that resets all your buttons to their original defaults. If not, you may have to write a separate procedure for each button to tell it to do what it used to do before.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: Function Keys

Postby jetjockey » Sat Jan 14, 2012 3:31 pm

Thanks this works,

Q1: So what would be the procedure to get it back when the workbook closes.

Q2 : How do I make a separate procedure for each key you want to assign a function to, and a separate OnKey call for it in the same Open event procedure.

Thanx
  • 0

jetjockey
Rookie
 
Posts: 12
Joined: Jan 14, 2012
Location: Trenton On
Reputation: 0

Re: Function Keys

Postby jetjockey » Sat Jan 14, 2012 4:56 pm

Hi,

As you have mentioned, I can run multiple application.Onkey and reset all the F keys.

Application.OnKey "{F1}", "ShowSheet1"

Now I need to do "Control+F1" for the next button and I was expecting to use :

Code: Select all
Application.OnKey "^{F1}", "ShowSheet4"



and its not working... Any Ideas

By the way I will not need to close the procedure as I need this to be valid as long as the workbook is open.

Thanks in advance you have been a great help
  • 0

jetjockey
Rookie
 
Posts: 12
Joined: Jan 14, 2012
Location: Trenton On
Reputation: 0

Re: Function Keys

Postby Sisyphus » Sun Jan 15, 2012 2:41 am

Hi,
If you put the line of code in the Open Event procedure you know that it runs because you tested that. When it runs it will call the procedure 'ShowSheet4'. If that procedure doesn't work you need to post it in order for some one to have a look at it.
BTW: procedures aren't like files that you may decide to 'keep open'. Procedures perform actions, like opening or closing files, or change the functionality of your computer. Doing that is serious business because you may well destroy your computer in the process or render it useless.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 213 guests