Last Used Cell in sheet

后端 未结 3 868
名媛妹妹
名媛妹妹 2021-01-21 09:59

I\'m new here and I\'m looking to use Excel VBA to return the last used cell in a worksheet.

I\'vv looked at Error in finding last used cell in Excel with VBA) but that d

3条回答
  •  隐瞒了意图╮
    2021-01-21 10:26

    Use Find both by row and column to identify this cell.

      Sub GetLastCellRange()
            Dim rng1 As Range
            Dim rng2 As Range
            Dim rng3 As Range
            Set rng1 = Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious)
            Set rng2 = Cells.Find("*", [a1], xlFormulas, , xlByColumns, xlPrevious)
            If Not rng1 Is Nothing Then
                Set rng3 = Range([a1], Cells(rng1.Row, rng2.Column))
                MsgBox "Range is " & rng3.Address(0, 0)
                'if you need to actual select the range (which is rare in VBA)
                Application.Goto rng3 
            Else
                MsgBox "sheet is blank", vbCritical
            End If
        End Sub
    

提交回复
热议问题