Solved

Set sequence of worksheets in WorkBook_Open

Posted on 2012-04-02
2
255 Views
Last Modified: 2012-04-02
Hello Experts,

I have a workbook for distribution - I do not need to personalize the ribbon, but I do want to keep the worksheets in the same sequence.  If the end-user should move them around - I would like to have them reset to my set sequence on Workbook_Open.

They do not fall in alphabetic order and there are a few sheets that I want out of numeric sequence.  I have 17 sheets - I do not mind having it layed out in 17 steps on Open to put them in sequence.

Any help would be appreciated,
Thanks
0
Comment
Question by:mike637
2 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
This is doable, though you can achieve the same thing (keep the user from moving sheets) via workbook protection.  Here's the code that goes in ThisWorkbook:

Private Sub Workbook_Open()
    Call reSequenceSheets
End Sub

Open in new window


And, the simple sort algorithm will be to start with the last sheet, end with the first sheet in sequence, and then just move that sheet before sheet 1.  At the end, the sheets are sorted in the sequence you desire.

I put the sheet sequence in the code, though this could easily be read in from a spreadsheet, as well.  Given there are only 17 to worry about, setting a comma delimited constant with the sheet names in the correct order seemed a good approach.

So, just change the constant, below, to be the order you want to retain the sheets in on the open event.

The following goes in a public module:

Option Explicit
Const shtOrder = "Sheet1,Sheet2,Sheet3,Sheet4,Sheet5,Sheet6,Sheet7,Sheet8,Sheet9,Sheet10,Sheet11,Sheet12,Sheet13,Sheet14,Sheet15,Sheet16,Sheet17"
Sub reSequenceSheets()
Dim wkb As Workbook
Dim vSheets As Variant
Dim i As Long

    Application.ScreenUpdating = False
    
    Set wkb = ThisWorkbook
    
    vSheets = Split(shtOrder, ",")
    
    For i = UBound(vSheets) To LBound(vSheets) Step -1
        wkb.Sheets(vSheets(i)).Move before:=wkb.Sheets(1)
    Next i

    Application.ScreenUpdating = True
    
End Sub

Open in new window


Download the attached, reorder the sheets and save.  When you open the file, the sheets should be back to the correct order.

Enjoy!

Dave
reSequenceSheetsOnOpen-r1.xlsm
0
 

Author Closing Comment

by:mike637
Comment Utility
Thank you very much for your guidance and expertise.

Michael
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now