Last Used Cell in sheet

后端 未结 3 1340
栀梦
栀梦 2021-01-21 09:44

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:40

    Did you try this?

    Dim r As Range
    Set r = Sheet1.UsedRange.SpecialCells(xlCellTypeLastCell)
    Debug.Print r.Address
    

    Output for your example:

    $D$4

    The UsedRange is known to not always match the actually used data range. Some workaround would be to use CurrentRegion:

    Dim r As Range
    With Sheet1.Range("A1").CurrentRegion
        Set r = Sheet1.Cells(.Rows.count, .Columns.count)
    End With
    Debug.Print r.Address
    

    also if the data does not start at A1, maybe this:

    With Sheet1.Cells.Find("*").CurrentRegion
    

提交回复
热议问题