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