Excel VBA: Get Last Cell Containing Data within Selected Range

后端 未结 3 558
清酒与你
清酒与你 2021-01-06 04:51

How do I use Excel VBA to get the last cell that contains data within a specific range, such as in columns A and B Range(\"A:B\")?

3条回答
  •  情书的邮戳
    2021-01-06 05:26

    For a variable selection you can use

    Sub test()
        Dim arrArray As Variant
        Dim iAct As Integer
        Dim iHighest As Integer
        arrArray = Split(Selection.Address(1, 1, xlR1C1), ":")
        For Count = Right(arrArray(0), 1) To Right(arrArray(1), 1)
            iAct = ActiveSheet.Cells(Rows.Count, Count).End(xlUp).Row
            If iAct > iHighest Then iHighest = iAct
        Next Count
        MsgBox iHighest
    End Sub
    

提交回复
热议问题