This should work
Public Sub SortRegions()
'#
'# declare private variables
'#
Dim pvt_lng_RegioStartRow As Long
Dim pvt_lng_LineNumber As Long
'#
'# initialise
'#
pvt_lng_RegioStartRow = 8
'#
'# loop for all rows on the current worksheet and identify the start and end rows of
'# each region - once identified the rows within the region will be sorted on column BX
'#
With ActiveSheet
For pvt_lng_LineNumber = 8 To (.Cells(.Rows.Count, "A").End(xlUp).Row + 1)
'#
'# if the contents of the cell in column A starts with the literal text "TOTAL" the
'# last line of a region has been encountered
'#
If Left$(.Cells(pvt_lng_LineNumber, "A").Value, 5) = "TOTAL" Then
'#
'# sort the region data
'#
With .Sort
.SortFields.Clear
.SortFields.Add _
Key:=Range(Cells(pvt_lng_RegioStartRow, "BX"), Cells(pvt_lng_LineNumber - 1, "BX")), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range(Cells(pvt_lng_RegioStartRow, "A"), Cells(pvt_lng_LineNumber - 1, "BX"))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'#
'# the starting row for the next region is the current line number + 1
'#
pvt_lng_RegioStartRow = pvt_lng_LineNumber + 1
End If
Next pvt_lng_LineNumber
End With
End Sub
Bookmarks