当列具有可变行数时,如何检索列中的最后一个值?
我想到了如何得到行号,例如:
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
如何使用它来获取单元格中的值?
例如if lastRow = 8794
,我想获得单元格K8794
的价值。
尝试
range("K" & lastRow).value
获得lastRow的值后
只是为了抛出另一个选择 - 你可以使用Range
变量来获取单元格,然后从那里你可以轻松地获取.Row
或.Value
(或任何其他Range
属性):
Dim lastCell as Range
Set lastCell = Sheets("Sheet1").Cells(Rows.Count,"K").End(xlUp)
Debug.Print "Last cell's row is: " & lastCell.Row
Debug.Print "Last cell's value is: " & lastCell.Value
Debug.Print "Last cell's address is: " & lastCell.Address
您可以通过多种方式获取最后一个单元格的值,其值为K列...
Dim lastRow As Long
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
MsgBox Range("K" & lastRow).Value
'OR
MsgBox Cells(lastRow, "K").Value
'OR
MsgBox Cells(lastRow, 11).Value 'here 11 is the column index for column K
用这个:
Sub ok()
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
Debug.Print Cells(lastRow, 11).Value
End Sub
从@ScottCraner留下的注释中检索列中最后一个值的解决方案:
单元格(Rows.Count,“K”)。End(xlUp).Value