Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Set sequence of worksheets in WorkBook_Open

Posted on 2012-04-02
2
Medium Priority
?
262 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 42

Accepted Solution

by:
dlmille earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

886 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