Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add worksheet after the last sheet of the workbook?

Tags:

excel

delphi

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:

enter image description here

Does anyone know how to add a worksheet after the last sheet of the workbook ?

like image 816
joostmakaay Avatar asked Dec 12 '13 14:12

joostmakaay


People also ask

How do you add a new worksheet at the end of the current 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.

How do you copy a worksheet and place it at the end of the workbook?

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.

What is the last worksheet in a workbook?

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.

How do I navigate to the next worksheet in a workbook?

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.


1 Answers

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]);
like image 170
Rob Kennedy Avatar answered Nov 12 '22 20:11

Rob Kennedy