Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Disabling Moving Between Worksheets.

Disabling Moving Between Worksheets

Written by Allen Wyatt (last updated December 19, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


1

Excel provides a variety of ways that you can move from one worksheet to another in a workbook. If you want to disable moving between worksheets, you've got a difficult task in front of you because of the variety of methods you need to do something about.

For instance, one way to move between worksheets is to press Ctrl+Page Up or Ctrl+Page Down. To disable these keys for a particular workbook, you need to use the OnKey method, in the following manner:

Private Sub Workbook_Activate()
    Application.OnKey "^{PgDn}", ""
    Application.OnKey "^{PgUp}", ""
End Sub
Private Sub Workbook_Deactivate()
    Application.OnKey "^{PgDn}"
    Application.OnKey "^{PgUp}"
End Sub

These two macros should be placed in the ThisWorkbook object. The first is run whenever the workbook is activated and it disables Ctrl+Page Up and Ctrl+Page Down by having nothing run when they are pressed. The second macro is run when the workbook is deactivated and re-enables the keys.

There are still a number of other ways to switch between worksheets, such as manually selecting the sheet, using Go To, using hyperlinks, etc. The easiest way to prevent moving between worksheets is to hide the worksheets you don't want accessed. Protecting the workbook and protecting the VB project will also aid in "thwarting" the user from moving between sheets.

If the sheets are hidden, they cannot be selected and thus you cannot move to them. Go To will not go to them, hyperlinks will not go to them. If you want users to be able to view the hidden worksheets later, you must create a macro routine with your own controls/buttons to go to those sheets. This routine would "unhide" the sheet you are going to and hide the one you just left.

Depending on your needs, there is one other approach you can try. You could add the following macro to the ThisWorkbook object:

Private Sub Workbook_SheetDeactivate(ByVal mySheet As Object)
    Application.EnableEvents = False
    mySheet.Activate
    Application.EnableEvents = True
End Sub

This macro is executed every time the current worksheet is deactivated. It essentially "reactivates" the worksheet that is being left, which means that no other worksheet can ever be selected.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11210) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Disabling Moving Between Worksheets.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Formatting Axis Patterns

Create a chart in Excel can you can then modify it almost any way you desire. One modification is to adjust the color or ...

Discover More

Double-Clicking to Widen Columns Won't Work

One way you can widen the columns in a worksheet to fit whatever is in the column is by double-clicking the right edge of ...

Discover More

Deleting All Tab Stops

Tab stops can be helpful when you want to align text within a paragraph. However, you might also want an easy way to get ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Ordering Worksheets Based on a Cell Value

Need to sort your worksheets so that they appear in an order determined by the value of a cell on each worksheet? Using a ...

Discover More

Using Very Long Worksheet Tab Names

Excel places a limit on how many characters you can use in a worksheet name. This tip discusses that limit and provides ...

Discover More

Protecting a Single Worksheet

Excel allows you to protect your worksheets easily, and that includes if you need to protect only a single worksheet out ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 3?

2020-12-19 09:41:45

David Bonin

I have many workbooks with VBA for my administrative use, and to gather and process data.
This code is of no value to those who receive my results, and it may be harmful if they try to use it.

I call the following function from many VBA procedures to thwart others from running my code. When the function returns a false, I shut down the calling routine.

It's not fool-proof, but it hasn't let me down yet. Those that try to work around this code and end up breaking things up can never come back to me and say it was an accident or that my code is buggy.

[[CODE START]]
Function IsAuthorizedUser(Optional ByVal BeChatty As Boolean = True) As Boolean

' Purpose: Determine whether the user is authorized to use a particular button.
' Present an error message for everyone else.
'
' Arguments: BeChatty (Optional)
' True - Provide a message to unauthorized users. This is the default.
' False - Do not provide a message to unauthorized users.
'
' Notes: Returns True if authorized. Returns False if not.
'

If Application.UserName = "Dave Bonin") Then
IsAuthorizedUser = True

Else
IsAuthorizedUser = False
If BeChatty = True Then
MsgBox "Only Dave Bonin is allowed to use" & vbCr & "this button. You are not him.", _
vbOKOnly + vbCritical, "You Are Not Authorized"
End If
End If
End Function
[[CODE END]]


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.