I'm generating an Excel sheet with Delphi, but the problem I'm facing is that when I use this code:
Workbook.Sheets.Add;
the new sheet is added before the currently selected worksheet:
Does anyone know how to add a worksheet after the last sheet of the workbook ?
To quickly insert a new worksheet at the end of the existing worksheets, click the Insert Worksheet tab at the bottom of the screen. To insert a new worksheet in front of an existing worksheet, select that worksheet and then, on the Home tab, in the Cells group, click Insert, and then click Insert Sheet.
On the Edit menu, click Sheet > Move or Copy Sheet. On the To book menu, click the workbook that you want to copy the sheet to. Tip: To create a new workbook that contains the moved sheet, click new book. In the Before sheet box, click the sheet that you want to insert the copied sheet before, or click move to end.
Worksheets(1) is the first (leftmost) worksheet in the workbook, and Worksheets(Worksheets. Count) is the last one. All worksheets are included in the index count, even if they are hidden. The worksheet name is shown on the tab for the worksheet.
Open the Navigation paneIn an open workbook, select View > Navigation. The Navigation pane will open on the right side of the window. The Navigation pane can also be opened from the status bar at the bottom of the screen. Right-click on the status bar and select Sheet Number.
The Add
method accepts parameters for you to indicate where to insert the new sheet, in particular the After
parameter. It should be a reference to the sheet to insert the new sheet after. In your case, you want the last sheet. Worksheets
is a 1-based array, so the last sheet is given by Worksheets[Worksheets.Count]
. Putting it all together, we get this:
Workbook.Worksheets.Add(After := Workbook.Worksheets[Workbook.Worksheets.Count]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With