Этот UDF вернет объект диапазона, представляющий диапазон сортировки, определенный первой строкой данных в пятом столбце, содержащем «белое» внутреннее пространство (не включая первую строку, если только это не ЕДИНСТВЕННАЯ строка с белым внутренним пространством) :
Option Explicit
Private Function SortRange(baseRange As Range) As Range
Dim firstCell As Range, lastCell As Range, C As Range
With baseRange
Set lastCell = .Worksheet.Cells(.Row + .Rows.Count - 1, .Column + .Columns.Count - 1)
End With
With Application.FindFormat
.Clear
.Interior.Color = vbWhite
End With
'find the first cell in fifth column that is white
'As implemented, this should be column E, but if you move the range, it will adjust to the 5th column
'after the first row in baseRange
With baseRange
Set firstCell = .Columns(5).Find(what:="*", after:=.Cells(1, 5), searchorder:=xlByRows, searchdirection:=xlNext, searchformat:=True)
If Not firstCell Is Nothing Then
With .Worksheet
Set SortRange = .Range(.Cells(firstCell.Row, 1), lastCell)
End With
Else
MsgBox "no cells to sort"
Exit Function
End If
End With
End Function
Вы можете использовать его следующим образом:
.SetRange SortRange(activesheet.range("A3:O150"))
, но я бы предложил заменить activesheet
на фактический лист.