# Sorting the Sheet tabs in Spreadsheet in Numerical Order [closed]

How can I sort sheets (tabs) in numerical order.

edit retag reopen merge delete

### Closed for the following reason question is not relevant or outdated by Alex Kemp close date 2020-08-02 12:04:36.326680

Hello @Siskar63,

The Sheets are already sorted in numerical order, ranging from index=0 to the total amount of Sheets minus 1.

Do you want to sort the Sheets based on a number which occurs in their names ?

( 2017-09-13 00:12:42 +0200 )edit

Sort by » oldest newest most voted

Hallo

In Addition to the solutions in the link a solution to sort Sheet-tabs in Natural_sort_order

import re
rex = re.compile(r'(\d+|\D+)')

def natsort(stext):
return [int(token) if token.isdigit()
else token
for token in rex.findall(stext)]

def sort_sheets_by_Name():
doc = XSCRIPTCONTEXT.getDocument()
sheets = doc.Sheets
sheetnames = sheets.ElementNames
for i, sheetname in enumerate(sorted(sheetnames, key=natsort)):
sheets.moveByName(sheetname, i)

more

This looks even better than the solutions in the link. Can't upvote though -- not enough rep.

( 2017-09-15 19:50:57 +0200 )edit

To sort sheets by name, run this macro from https://forum.openoffice.org/en/forum....

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
End If
Sheets = ThisComponent.Sheets
Names = Sheets.ElementNames
SortedList = BubbleSortList(Names) 'BubbleSortList is a macro in the Tools Library'
for i = 0 to UBound(SortedList)
Sheets.MoveByName(SortedList(i), i)
next i

more

Hi @Jim K my edit is only to add ' (end of comment : no need in basic of course, only here...)

Regards

( 2017-09-14 07:33:34 +0200 )edit

@pierre-yves samyn: Thank you; I couldn't figure out how to make it look right.

( 2017-09-14 11:07:40 +0200 )edit

@Jim K :why not quote the python-solutions from the same Thread

( 2017-09-14 11:43:25 +0200 )edit

@karolus: Because it's quicker for a typical user to copy and paste a Basic macro into the IDE than to set up a Python script. If the question was about macro programming, then I may have suggested Python, which is what I use most.

( 2017-09-15 19:33:00 +0200 )edit

Worked for me.. thanks!

( 2017-12-27 11:19:15 +0200 )edit