+ Reply to Thread
Results 1 to 5 of 5

VBA Sheets Order

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Sheets Order

    Hi, I wonder whether someone may be able to help me please.

    I'm using the code below to add multiple sheets to a workbook:

    Sub NewSheets()
        
        Dim shArray() As Variant             'Declare the sheet Name array and a
        Dim i As Long                        'counter variable
        
            shArray = Array("BAS Consultants", _
                "Monthly Direct", _
                "Monthly Enhancements", _
                "Monthly Indirect", _
                "Monthly Overheads", _
                "Monthly Projects", _
                "Yearly Direct", _
                "Yearly Enhancements", _
                "Yearly Indirect", _
                "Yearly Overheads", _
                "Yearly Projects", _
                "C&R In Flight Projects", _
                "S&A In Flight Projects", _
                "C&R Graph Data", _
                "S&A Graph Data", _
                "C&R Flexible Resource Profile", _
                "S&S Flexible Resource Profile", _
                "C&R Flexible Resource KPI", _
                "C&R Resource Capacity")    'Populate the array
                
                For i = LBound(shArray) To UBound(shArray)  'Loop through the elements
                    Sheets.Add(After:=Worksheets(1)).Name = shArray(i)
                    Next i
    End Sub
    The code works, but the issue I have is that the position of the sheets when the workbook has been built, doesn't reflect the order by which I've listed them in the code i.e. the "BAS Consultants" sheet becomes the last sheet in the workbook rather than the first.

    Could someone possibly tell me please, is there a way to force the sheets to be created in the position by which they are set in the list above.

    Many thanks and kind regards

    Chris

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA Sheets Order

                For i = LBound(shArray) To UBound(shArray)  'Loop through the elements
                    Sheets.Add(After:=Worksheets(Worksheets.count)).Name = shArray(i)
                Next i
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Sheets Order

    Hi @patel45, thank you for taking the time to reply to my post and for the help.

    The solution works great.

    Many thanks and kind regards

    Chris

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,696

    Re: VBA Sheets Order

    Put all the sheet names to be added in Column A (starting at Cell A1).

    Public Function WorkSheetExists(ByVal strName As String) As Boolean
       On Error Resume Next
       WorkSheetExists = Not Worksheets(strName) Is Nothing
    End Function
    Sub Try()
    Application.ScreenUpdating = False
    Dim c As Range
    For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Not WorkSheetExists(c.Value) Then
            Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value
        End If
    Next c
    Sheets("Sheet1").Select
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by jolivanes; 04-28-2014 at 02:01 AM. Reason: Patel45 was faster and more elegant.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Sheets Order

    Hi @jolivanes, thank you very much for taking the time to reply to my post and for the suggestion, it is much appreciated.

    Kind Regards

    Chris

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need help with inventory and order sheets
    By cdn_medic in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-11-2013, 10:17 PM
  2. Need help deleting sheets and moving existing sheets down in order
    By MAGICofSeth in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-27-2012, 06:13 PM
  3. Sorting the order of sheets by the name
    By Ctech in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2005, 12:45 PM
  4. Re: Sorting the order of sheets by the name
    By Norman Jones in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-25-2005, 08:50 AM
  5. order sheets
    By sal21 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-24-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1