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.
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
Will select up to 3 sheets to the right of the active sheet. Assumes no hidden sheets exist among those being selected.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
If there are hidden sheets then this will select up to 2 visible sheets to the right of the active sheet:
edit: didn't read the "and copy them to the end of the visible worksheets using VBA code", so: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
ps. Excel 2007 extension is xlsx/xlsm/xlsbSub 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
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.
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 )
p45cal :
Thanks.
( Somehow, my replies were NOT showing up. Not sure what's going on)
---------------------------------------------------------------------------------------------------------------------
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
Paul_Hossler
Agree.
Good programming practice.
Thanks