excel 从范围选择中选择最后一行

rvpgvaaj  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(151)

类似于我们如何使用range(b2:b & lastrow),我们如何使用Range(Selection,Selection.End(xlDown)).Select & Lastrow来实现这一点。所以我想转到B1,控制箭头向下,然后控制换档箭头向下。我不想使用控件shift箭头向下,而是想使用LastRow实现结果。有什么办法吗?谢谢

zsbz8rwp

zsbz8rwp1#

这就是你想要的吗?

Sub Select_last_rowrange_selection()
    Dim lastrow As Long
    lastrow = Range("b1").Worksheet.UsedRange.Rows.Count
    
    'this will select the column B used range
    Range("b2:b" & lastrow).Select
    
    'this will select the last row of column B
    Range("b" & lastrow).Select
End Sub

Sub Select_last_row_from_range_selection()
    Dim lastrow As Long, rng As Range
    Set rng = Selection
    lastrow = rng.Rows.Count
    'this will select the last row of selected range
    rng.Rows(lastrow).Select
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    rem select only one cell
    If Target.Cells.Count = 1 Then
'        ...
    rem select more than one cells
    Else
'        ...
    End If
End Sub

相关问题