Solved

Set sequence of worksheets in WorkBook_Open

Posted on 2012-04-02
2
256 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
ID: 37797740
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
ID: 37798667
Thank you very much for your guidance and expertise.

Michael
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

911 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

22 Experts available now in Live!

Get 1:1 Help Now