1

I have an Excel file with many rows and it gets many entries daily. That's why I would like to scroll down the file to last row with data.

To find the last row, I use:

last_row = workbook.sheets[0].range('A1').end('down').row 

How could I scroll down to it using Python (and preferably XLWings module)?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
gunardilin
  • 349
  • 3
  • 12
  • 1
    Just an observation. `End('down')` get the last row with data **in the current region**. So if you have a blank row and then a new range of data, you are not getting the real last row of data. Check [Range.End property](https://learn.microsoft.com/en-us/office/vba/api/excel.range.end) and check https://stackoverflow.com/a/38882823/9199828 I know it's VBA but properties work the same in Python – Foxfire And Burns And Burns Apr 20 '22 at 12:39

1 Answers1

2

Use used_range, it's more robust than expand("down) or end("down"), if you have empty cells in the table. Here is the code:

import xlwings as xw

path = r"test.xlsx"

wb = xw.Book(path)
ws = wb.sheets[0]
ws.used_range[-1:,:].select()

I have used an index on the range object [-1:,:], the numbers before the comma represent the start range cell, the numbers after the comma represent the end range cell. If you would like to select the single cell of the last row in the first column used_range[-1:,:][0] would do it.

mouwsy
  • 1,457
  • 12
  • 20