Consulting

Results 1 to 9 of 9

Thread: Need Help: Changing Sheet Index Property in VBA code

  1. #1

    Need Help: Changing Sheet Index Property in VBA code

    Hi,

    I would like to have a code to change the sheet index property through VBA, I tried the below but it didn't work:

    Sheets("Sales").CodeName = "Sheet2"
    Sheets("Sales").Index = "Sheet2"

    Please help, appreciate.

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    The Index is determined by the sheet's postition in the tab order. To change it, move the sheet.
    Be as you wish to seem

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Also .Index is a number, not a string.

    By any chance, do you want to change the .Name of the sheet?

    Oherwise you have to use something like

    Sheet2.Move after:= Worksheets(Worksheets.Count)

    to move it to the end
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sheets("Sales").CodeName = "Sheet2"
    Sheets("Sales").Name = "Sheet2"
    
    NewIndex = 3
    Sheets("Sheet2").Move After:=Sheets(NewIndex)
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Dear all,
    I tried the above but still not working.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       on error resume next
       Sheets("Sales").move sheets(Sales").index-1)
    end sub
    Sub M_snb()
       on error resume next
       Sheets("Sales").move ,sheets(Sales").index+1)
    end sub

  7. #7
    Sir,

    The above code is giving error message.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. What is the error message?

    2. "I would like to have a code to change the sheet index property through VBA, I tried the below but it didn't work:"

    You can't just change the .Index. You need to move the sheet


    3. Try something like

    Option Explicit
    
    Sub MoveSheet()
        Dim i As Long
        
        i = Worksheets("Sheet2").Index
        'move sheet4 BEFORE sheet2
        If i - 1 = 0 Then
            MsgBox "cannot move before"
        Else
            Worksheets("Sheet4").Move before:=Worksheets(i)
        End If
    
    
        i = Worksheets("Sheet3").Index
        'move sheet1 AFTER sheet3
        If i + 1 = Worksheets.Count Then
            MsgBox "cannot move after"
        Else
            Worksheets("Sheet1").Move after:=Worksheets(i)
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    The real goal of OP is unclear to me.
    Changing of the sheet's index by moving sheet is described here.
    The below is example of changing codenames at runtime
    Sub Test()
      
        Dim Sh As Worksheet
        
       ' There should be only one (unique) "Sheet2" codename in the workbook.
       ' Try to rename codename of the present "Sheet2" to the unique name
        On Error Resume Next
        Set Sh = Sheets("Sheet2")
        If Sh.CodeName = "Sheet2" Then Sh.[_CodeName] = "Sheet" & Replace(Trim(Str(CDbl(Now))), ".", "")
        On Error GoTo 0
        
        ' Change codename of the Sheets("Sales")
        Set Sh = Sheets("Sales")
        Sh.[_CodeName] = "Sheet2"
      
    End Sub

Posting Permissions

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