Consulting

Results 1 to 9 of 9

Thread: VBA Code to select specific sheet from the ActiveSheet

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location

    VBA Code to select specific sheet from the ActiveSheet

    In Excel 2007, so an xls file, How to select the ActiveSheet plus the two sheets on the right, ( 3 Sheets ) and copy them to the end of the visible worksheets using VBA code.
    Cant Hardcode the Sheet Names because they change.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,874
    Location
    Quote Originally Posted by simora View Post
    In Excel 2007, so an xls file, How to select the ActiveSheet plus the two sheets on the right, ( 3 Sheets ) and copy them to the end of the visible worksheets using VBA code.
    Cant Hardcode the Sheet Names because they change.
    Are the visible worksheets in the ActiveWorkbook with the ActiveSheet?

    What happens if the ActiveSheet is so far to the right that it doesn't have 2 more sheets?

    So ...

    Sheet20
    Sheet15
    Sheet10 <<< ActiveWorksheet
    Sheet5
    Sheet6 <<<<< Hidden
    Sheet7
    Sheet8

    Ends up like

    Sheet20
    Sheet15
    Sheet6 <<< hidden
    Sheet8
    Sheet10 <<< ActiveWorksheet
    Sheet5
    Sheet7
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,967
    Sub blah()
        a = ActiveSheet.Index
        b = Application.Min(Sheets.Count, a + 2)
        For i = a + 1 To b
            Sheets(i).Select False
        Next i
    End Sub
    Will select up to 3 sheets to the right of the active sheet. Assumes no hidden sheets exist among those being selected.

    If there are hidden sheets then this will select up to 2 visible sheets to the right of the active sheet:
    Sub blah2()
        i = ActiveSheet.Index
        Do Until Z = 2 Or i = Sheets.Count
            i = i + 1
            If Sheets(i).Visible Then
                Z = Z + 1
                Sheets(i).Select False
            End If
        Loop
    End Sub
    edit: didn't read the "and copy them to the end of the visible worksheets using VBA code", so:
    Sub blah3()
        NoSTM = 3   'No. of Sheets To Move
        ReDim mySheets(1 To NoSTM)
        i = ActiveSheet.Index
        Z = 1
        Set mySheets(Z) = ActiveSheet
        Do Until Z = NoSTM Or i = Sheets.Count
            i = i + 1
            If Sheets(i).Visible Then
                Z = Z + 1
                Set mySheets(Z) = Sheets(i)
            End If
        Loop
        For j = 1 To Z
            mySheets(j).Move After:=Sheets(Sheets.Count)
        Next j
        mySheets(1).Activate
    End Sub
    ps. Excel 2007 extension is xlsx/xlsm/xlsb
    Last edited by Aussiebear; 01-10-2025 at 03:52 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Are the visible worksheets in the ActiveWorkbook with the ActiveSheet?

    What happens if the ActiveSheet is so far to the right that it doesn't have 2 more sheets?

    So ...

    Sheet20
    Sheet15
    Sheet10 <<< ActiveWorksheet
    Sheet5
    Sheet6 <<<<< Hidden
    Sheet7
    Sheet8

    Ends up like

    Sheet20
    Sheet15
    Sheet6 <<< hidden
    Sheet8
    Sheet10 <<< ActiveWorksheet
    Sheet5
    Sheet7

  5. #5
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    Paul_Hossler : There will ALWAYS be 3 or more visible sheets to the right of the Active Sheet.

    ( Somehow, my replies were NOT showing up. Strange )

  6. #6
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    p45cal :
    Thanks.

    ( Somehow, my replies were NOT showing up. Not sure what's going on)

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,874
    Location
    Quote Originally Posted by simora View Post
    Paul_Hossler : There will ALWAYS be 3 or more visible sheets to the right of the Active Sheet.
    In my experience that's true unti it's not

    I try to use defensive programming just in case, but that's just me
    ---------------------------------------------------------------------------------------------------------------------

    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

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,967
    Quote Originally Posted by simora View Post
    p45cal :
    Thanks.

    ( Somehow, my replies were NOT showing up. Not sure what's going on)
    I've just realised that I may still have misread the question: you want the sheets copied to the end, not moved to the end?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    Paul_Hossler


    Agree.
    Good programming practice.
    Thanks

Posting Permissions

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