我相信这样做的最安全方法是使用API
属性,而只需使用VBA
Range.Find
方法。现在,我们可以使用Range.Find
通配符搜索任何内容,以确保找到公式(可以显示*
)和值。简化示例:
""
我们可以使用import xlwings as xw
app = xw.App(visible=False, add_book=False)
wb = app.books.add()
ws = wb.sheets.active
ws['C2'].value = 'check'
row_cell = ws.api.Cells.Find(What="*", After=ws.api.Cells(1, 1), LookAt=xw.constants.LookAt.xlPart, LookIn=xw.constants.FindLookIn.xlFormulas, SearchOrder=xw.constants.SearchOrder.xlByRows, SearchDirection=xw.constants.SearchDirection.xlNext, MatchCase=False)
column_cell = ws.api.Cells.Find(What="*", After=ws.api.Cells(1, 1), LookAt=xw.constants.LookAt.xlPart, LookIn=xw.constants.FindLookIn.xlFormulas, SearchOrder=xw.constants.SearchOrder.xlByColumns, SearchDirection=xw.constants.SearchDirection.xlNext, MatchCase=False)
print((row_cell.Row, column_cell.Column))
并从SearchDirection.xlNext
开始。这是Cells(1,1)
中的recommended方法,可以可靠地找到您最后使用的行/列,但也可以可靠地找到您的第一行/列。
以上内容只有在VBA
为空的情况下才是正确的!