I would like to create the equivalent of this VBA macro from excel in Xlwings:
Sub sortononecol()
Range("A4").CurrentRegion.Sort key1:=Range("A4"), order1:=xlAscending, Header:=xlYes
End Sub
I tried using the script below but got different results.
last_row = ws.range('B' + str(ws.cells.last_cell.row)).end('up').row
ws.range("A4:N{row}".format(row=last_row)).api.Sort(Key1=ws.range("A5:A{row}".format(row=last_row)).api, Order1=1, Header=1)
Does anyone know what I am doing wrong? Thanks!
Working from your VBA code;
You want to select cell 'A4' have the current region selected and sort ascending handling a Header row.
In this example, rows 1 to 9 in column 'A' with row 1 (cell 'A1') as header. Current region from cell 'A4' is 'A1:A9'.
...
col_range = ws.range('A4').current_region # using cell coordinate
# col_range = ws.range(4, 1).current_region # using row/col coordinate
ws.range(col_range).api.Sort(Key1=col_range.api, Order1=1, Header=1, Orientation=1)