Option Explicit
Function UsedRegion(Optional StartCell As Range) As Range
Dim LastCol&, LastRow&, Junction As Range
On Error GoTo NotFound
LastCol = Cells.Find("*", SearchOrder:=xlByColumns, _
LookIn:=xlValues, SearchDirection:=xlPrevious).Column
LastRow = Cells.Find("*", SearchOrder:=xlByRows, _
LookIn:=xlValues, SearchDirection:=xlPrevious).Row
If StartCell Is Nothing Then Set StartCell = [A1]
If StartCell.Row > LastRow Or StartCell.Column > LastCol Then
MsgBox "Your start cell is outside the used region"
End
Else
Set Junction = Intersect(Columns(LastCol), Rows(LastRow))
Set UsedRegion = Range(StartCell.Address & ":" & Junction.Address)
End If
Exit Function
NotFound:
MsgBox "There are no values on this sheet", , "No Used Region"
Application.DisplayAlerts = False
End
End Function
Sub UsedRegionAddy()
MsgBox "The Used Region address is " & UsedRegion.Address
End Sub
Sub EntireUsedRegion()
UsedRegion.Interior.ColorIndex = 35
End Sub
Sub TheUsedRegion()
If Selection.Cells.Count > 1 Then
MsgBox "Please select one cell only...", , "Ambiguous - Need One Cell"
Exit Sub
End If
UsedRegion(ActiveCell).Select
If Selection.Cells.Count = 1 Then
MsgBox "(Only one cell in region)"
End If
End Sub
|