Skip to content

Instantly share code, notes, and snippets.

@Elijas
Last active January 22, 2022 21:00
Show Gist options
  • Save Elijas/2430813d3ad71aebcc0c83dd1f130e33 to your computer and use it in GitHub Desktop.
Save Elijas/2430813d3ad71aebcc0c83dd1f130e33 to your computer and use it in GitHub Desktop.
xlwings find used range boundaries (last used row and last used column)
import xlwings as xl
import xlwings.constants
s = xl.Book('doc.xlsx').sheets[0]
RR2 = s.api.Cells.Find(What="*",
After=s.api.Cells(1, 1),
LookAt=xlwings.constants.LookAt.xlPart,
LookIn=xlwings.constants.FindLookIn.xlFormulas,
SearchOrder=xlwings.constants.SearchOrder.xlByRows,
SearchDirection=xlwings.constants.SearchDirection.xlPrevious,
MatchCase=False)
RR = s.api.Cells.Find(What="*",
After=s.api.Cells(1, 1),
LookAt=xlwings.constants.LookAt.xlPart,
LookIn=xlwings.constants.FindLookIn.xlFormulas,
SearchOrder=xlwings.constants.SearchOrder.xlByColumns,
SearchDirection=xlwings.constants.SearchDirection.xlPrevious,
MatchCase=False)
print((RR2.Row, RR.Column))
pass
@bsyouness
Copy link

bsyouness commented May 9, 2017

Hey!

I found this code while trying to find a way to select the used range of a sheet. I came up with something I thought could be useful to others:

import xlwings as xw

out_wb = xw.Book(r"x:\EPA_MPG\not_matched_comparator.xlsm")

active_sheet = out_wb.sheets.active
used_range_rows = (active_sheet.api.UsedRange.Row, 
	active_sheet.api.UsedRange.Row + active_sheet.api.UsedRange.Rows.Count)
used_range_cols = (active_sheet.api.UsedRange.Column, 
	active_sheet.api.UsedRange.Column + active_sheet.api.UsedRange.Columns.Count)
used_range = xw.Range(*zip(used_range_rows, used_range_cols))
used_range.select()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment