Hi I would like to get the bounds of a range in an efficient manner and was wondering if anyone knew of some properties or functions that could do this. i have included a sample function of what I would like to accomplish, except I don't want to loop through the elements as the range could be quite large.
Sub test()
Dim rng As Range, cell As Range, minCol As Long, minRow As Long, maxRow As Long, maxCol As Long
Set rng = Union(Range("G10"), Range("I11"), Range("F20"), Range("K24"))
minCol = Columns.Count
minRow = Rows.Count
maxRow = 0
maxCol = 0
For Each cell In rng
minCol = Application.WorksheetFunction.Min(minCol, cell.Column)
minRow = Application.WorksheetFunction.Min(minRow, cell.Row)
maxCol = Application.WorksheetFunction.Max(maxCol, cell.Column)
maxRow = Application.WorksheetFunction.Max(maxRow, cell.Row)
Next cell
Dim rngRectangular As Range
Set rngRectangular = Range(Cells(minRow, minCol), Cells(maxRow, maxCol))
End Sub
thanks,
Taylour
Sub test()
Dim rng As Range, cell As Range, minCol As Long, minRow As Long, maxRow As Long, maxCol As Long
Set rng = Union(Range("G10"), Range("I11"), Range("F20"), Range("K24"))
minCol = Columns.Count
minRow = Rows.Count
maxRow = 0
maxCol = 0
For Each cell In rng
minCol = Application.WorksheetFunction.Min(minCol, cell.Column)
minRow = Application.WorksheetFunction.Min(minRow, cell.Row)
maxCol = Application.WorksheetFunction.Max(maxCol, cell.Column)
maxRow = Application.WorksheetFunction.Max(maxRow, cell.Row)
Next cell
Dim rngRectangular As Range
Set rngRectangular = Range(Cells(minRow, minCol), Cells(maxRow, maxCol))
End Sub
thanks,
Taylour