如何通过Excel COM接口获取选定的行列数量?

1 投票
1 回答
920 浏览
提问于 2025-04-17 19:28

我可以通过应用程序的 .Selection 属性获取用户选中的区域。

(我正在使用 Python 来访问 Excel 的 COM 接口)

我想知道用户选中了多少行和多少列,比如说:

input:
    user has selected A1:G8
output:
    the selected range starts from Column 1 (by selection.Column)
    the selected range starts from Row 1 (by selection.Column)
    the selected range is spanning 7 rows (by ?)
    the selected range is spanning 8 columns (by ?)

我在查看 MSDN 的 Range 接口,但是这个属性似乎对解决这个问题没有帮助。

1 个回答

0

从VBA(Visual Basic for Applications)中,你可以这样做:

Debug.Print Selection.Rows.Count
Debug.Print Selection.Columns.Count
Debug.Print Selection.Row
Debug.Print Selection.Column

当你选中A1到G8这个区域时,这段代码会返回

8 
7 
1 
1 

这个可以很容易地转换成Python吗?

撰写回答