VBA - Using OnKey and CTRL+SHIFT+PAGE UP

MichaelB1

New Member
Joined
Nov 4, 2013
Messages
32
Hi All,

Newbie to the forum but a long-time reader of certain parts - thank you to all contributors as you've really helped me in the past.

As part of a monster model that I've been asked to build, I've been trying to pull together a couple of Macros to help other users moving around the workbook. My coding experience is limited and normally restricted to editing recorded macros, so this is a bit of a learning experience for me.

One Macro I'd like to build is an update on "CTRL+Page Up/Page Down" but changing it to move to the first sheet in the workbook or to the last. I've currently got this mapped to CTRL+SHIFT+N/M but (because it just makes more sense to me) I'd like to map it to CTRL+SHIFT+PgUp/PgDn.

The code I've been trying is (ignoring a few 'move to cell ref' bits)

Start Sub Cleverkeys()

Firstsheet = Activeworkbook.worksheets(1).Activate
Application.Onkey "^+{PgUp}", Firstsheet

End Sub()

However, I just can't get the Onkey line to function - I'm probably trying to mix apples and pears!

Can anyone help me to build this code? Oh, and lets assume that this ignores any hidden tabs in the workbook (if I do hide any, I won't want the users to find them!).

Thanks in advance



Michael
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You need to run Application.Onkey to hook those keys up to your macro, so you actually need three routines - one to hook the keys up, one for the key combo to actually run, and one to unhook the keys when you're done:
Code:
Sub Cleverkeys()

Activeworkbook.worksheets(1).Activate

End Sub

Sub HookKeys()
Application.Onkey "^+{PgUp}", "CleverKeys"
End Sub
Sub UnhookKeys()
Application.Onkey "^+{PgUp}"
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,034
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top