Click here to Skip to main content
15,885,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to move all the sheets in the food pantry archive file to reverse the order. I can move all the sheets except the end sheet, which is the first sheet to move. The Totals sheet, which is the first sheet, does not get moved.



C#
private void MoveArchiveDataSheets()
        {
            // Open the archive workbbook.
            theArchiveFile = frmFoodPantry.objApp.Workbooks.Open(strArchiveFileName);
            // The work is done so save the archive workbook.
            //See how many sheets there are including the totals sheet
            int numOfSheets = theArchiveFile.Sheets.Count;
            // Set the counter for the sheet to move before.
            int moveBefore = numOfSheets;

            // Select the first data sheet.
            theArchiveFile.Worksheets[2].Select();

            // Move the first data sheet to the end of the workbook
            ((Excel.Worksheet)frmFoodPantry.objApp.ActiveSheet).Move(
                frmFoodPantry.objApp.Worksheets[numOfSheets]);
            //            (theArchiveFile.ActiveSheet).Move(theArchiveFile.Worksheets[moveBefore]);
            //(theArchiveFile.ActiveSheet).Move(frmFoodPantry.objApp.Worksheets[moveBefore]);
            //frmFoodPantry.objApp.Sheets[2].Move(theArchiveFile.Sheets[numOfSheets]);
            //theArchiveFile.ActiveSheet.MoveToEnd(theArchiveFile.Sheets[numOfSheets]);
            //(frmFoodPantry.objApp.ActiveSheet).Move(frmFoodPantry.objApp.Worksheets[moveBefore]);
            //Loop through all the data sheets and move them
            for (int i = 2; i < numOfSheets; i++)
            {
                moveBefore-- ;
                // Select the next data sheet to move (it will always be sheet 2).
                theArchiveFile.Worksheets[2].Select();
                // Move the next data sheet to it's proper month sequence
                //(frmFoodPantry.objApp.ActiveSheet).Move(frmFoodPantry.objApp.Worksheets[moveBefore]);
                theArchiveFile.Worksheets[2].Move(theArchiveFile.Worksheets[moveBefore]);
            }
            theArchiveFile.Save();
            // The archive workbook has bee saved, and we don't need it open anmore, so close it.
            theArchiveFile.Close();

        }


What I have tried:

C#
((Excel.Worksheet)frmFoodPantry.objApp.ActiveSheet).Move(
                frmFoodPantry.objApp.Worksheets[numOfSheets]);
            //            (theArchiveFile.ActiveSheet).Move(theArchiveFile.Worksheets[moveBefore]);
            //(theArchiveFile.ActiveSheet).Move(frmFoodPantry.objApp.Worksheets[moveBefore]);
            //frmFoodPantry.objApp.Sheets[2].Move(theArchiveFile.Sheets[numOfSheets]);
            //theArchiveFile.ActiveSheet.MoveToEnd(theArchiveFile.Sheets[numOfSheets]);
            //(frmFoodPantry.objApp.ActiveSheet).Move(frmFoodPantry.objApp.Worksheets[moveBefore]);
            //Loop through all the data sheets and move them
Posted
Updated 12-Aug-22 8:43am
v3

1 solution

You need to capture the first sheet at the beginning and then move the other sheets - beginning with the most right - before that sheet.
This works for me:
private void buttonSheetReverseOrder_Click(object sender, EventArgs e)
{
    // Excel Application
    Microsoft.Office.Interop.Excel.Application _excel = new _Excel.Application();

    // No User Intervention Dialogues
    _excel.DisplayAlerts = false;

    // Open workbook
    Workbook workbook = _excel.Workbooks.Open(@"c:\temp\cp.excel\ExcelReverseSheetOrder.xlsx");


    // Reverse Seet Order
    Worksheet firstSheet = workbook.Sheets[1];
    for (int ix= workbook.Sheets.Count; ix > 1; ix--)
    {
        workbook.Sheets[workbook.Sheets.Count].Move(firstSheet);
    }

    // SaveAs the workbook. SaveAs makes test more easy ;)
    workbook.SaveAs(@"c:\temp\cp.excel\ExcelMovedSheets.xlsx");

    // Close
    workbook.Close();
}


[Edit]
In case we have the sheets 'Total', 'Dec' ..... 'Jan' and we need them to reorder to 'Total', 'Jan' ..... 'Dec' we can do it this way:
private void buttonSheetReverseOrder_Click(object sender, EventArgs e)
{
    // Excel Application
    Microsoft.Office.Interop.Excel.Application _excel = new _Excel.Application();

    // No User Intervention Dialogues
    _excel.DisplayAlerts = false;

    // Open workbook
    Workbook workbook = _excel.Workbooks.Open(@"c:\temp\cp.excel\ExcelReverseSheetOrder.xlsx");


    // Reverse Seet Order
    Worksheet firstSheet = workbook.Sheets[2];
    for (int ix = 13; ix > 2; ix--)
    {
        workbook.Sheets[workbook.Sheets.Count].Move(firstSheet);
    }

    // SaveAs the workbook
    workbook.SaveAs(@"c:\temp\cp.excel\ExcelMovedSheets.xlsx");

    // Close
    workbook.Close();
}
 
Share this answer
 
v2
Comments
PaulaJoannAllen 12-Aug-22 14:47pm    
The starting order is Totals, Dec, Nov, Oct ...., I need the order to be Totals, Jan, Feb, Mar ....
0x01AA 12-Aug-22 14:48pm    
That simply means Worksheet firstSheet = workbook.Sheets[2]; and for (int ix= workbook.Sheets.Count; ix > 2; ix--) I think. Please test it and give some feedback ;)
0x01AA 12-Aug-22 14:51pm    
See comment above. I just tried it and it works if make that two small changes.
PaulaJoannAllen 12-Aug-22 15:02pm    
Works like a charm, thank you.
0x01AA 12-Aug-22 15:05pm    
You are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900