excel - 在 VBA 中查找选定区域的最后一行和最后一列
问题描述
我试图找到我在工作表中选择的区域的最后一行和最后一列。
通常我用它来查找最后一行和最后一列:
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
但是,这些行适用于 Row = 1 和 Column = 1。
假设我在其他地方有一张桌子。我想选择表格并运行代码。然后我想要以下变量:
FirstRowTable =
LastRowTable =
FirstColumnTable =
LastColumnTable =
我尝试过这样的事情,但它不起作用:
LastRowTable = Selection.Row + Selection.Rows.Count - 1
FirstRowTable = Selection.Row
有任何想法吗?
解决方案
以下是Selected
矩形范围的一些参数:
Sub range_reporter()
Dim r As Range
Dim s As String
Dim nLastRow As Long, nLastColumn As Long
Dim nFirstRow As Long, nFirstColumn As Long
Set r = Selection
nLastRow = r.Rows.Count + r.Row - 1
MsgBox ("last row " & nLastRow)
nLastColumn = r.Columns.Count + r.Column - 1
MsgBox ("last column " & nLastColumn)
nFirstRow = r.Row
MsgBox ("first row " & nFirstRow)
nFirstColumn = r.Column
MsgBox ("first column " & nFirstColumn)
numrow = r.Rows.Count
MsgBox ("number of rows " & numrow)
numcol = r.Columns.Count
MsgBox ("number of columns " & numcol)
s = r.Address
MsgBox ("address " & s)
s = r(1).Address
MsgBox ("address of first cell " & s)
MsgBox ("worksheet " & r.Worksheet.Name)
MsgBox ("workbook " & r.Worksheet.Parent.Name)
MsgBox ("item count " & r.Count)
'
'sub-ranges
'
MsgBox "address of first row " & r.Rows(1).Address
MsgBox "address of first column " & r.Columns(1).Address
End Sub
推荐阅读
- php - 如何使用 PhpSpreadsheet 获取单元格内容的货币符号
- c++ - 为什么 std::cbegin() 不在容器上调用 .cbegin() ?
- coq - 了解专精战术
- c++ - 在 C++ 三元运算符中使用字符串常量是对非左值数组的无效使用吗?
- java - 多节点模块(Xodus)的循环依赖
- c++ - C++ 按字母顺序对向量的向量中的字符串进行排序
- r - 找到具有多个交点的两个图之间的公共区域
- reactjs - 仅显示某些路由的组件 - React
- javascript - 如何从 aws lambda 调用中获取完整日志?
- php - 目标返回 mysql lastInsertId 但是 insert 在 php 函数中创建挑战