Share via

move excel sheets

Jonathan Brotto 1,076 Reputation points
2022-01-16T04:19:44.18+00:00

I added a bunch of sheets in my vsto project and had them renamed but can't rearrange them . tried using some vba for inspiration.

        Dim addSheets As Excel.Sheets = Globals.ThisAddIn.Application.Sheets

        addSheets.Add()
        addSheets.Add()
        addSheets.Add()

        Dim sheetRename As Excel.Worksheet = Globals.ThisAddIn.Application.Sheets("Sheet1")
        sheetRename.Name = "Raw SAP"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet2")
        sheetRename.Name = "Territory Summary"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet3")
        sheetRename.Name = "Customer Summary"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet4")
        sheetRename.Name = "Product Summary"

        sheetRename = Globals.ThisAddIn.Application.Worksheets(1)

        sheetRename = Globals.ThisAddIn.Application.Sheets("Territory Summary")

        sheetRename.Move(After:=4)

System.Runtime.InteropServices.COMException
HResult=0x800A03EC
Message=Exception from HRESULT: 0x800A03EC
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>

Microsoft 365 and Office | Development | Other
Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-01-16T08:44:07.807+00:00

Try fixing the last line:

sheetRename.Move( After := Globals.ThisAddIn.Application.Worksheets(4) )

Maybe also redesign the program. For example, you can use the return values of addSheets.Add to access and rename the new sheets, and you can specify certain parameters for addSheets.Add.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.