Attached (rawsortexample.csv) is a spreadsheet that needs to be custom sorted by my VBA macro. Post macro, it should look like sortexampledone.csv
In column B, you see several different values in the format "Community" (URS, MN, ARB, etc) then an "Account number" (1234a, etc) then a date. I need to sort it so that the URS and MN communities are on top and everything else is on the bottom. My example shows ARB, CM and WRD communities but we are often adding new communities so I can't use the CustomOrder: method suggested by protonLeah above as we would need to be constantly updating the macro to include new communities. I was thinking something to the effect of pseudocode:
For Each cell In Range("A2:A" & Range("A1").CurrentRegion.Rows.Count)
If Left(cell, 2) = "MN" Then
Move cell.row to row.2
ElseIf Left(cell, 3) = "URS" Then
Move cell.row to row.2
Endif
Next cell
The communities will already be sorted by the following code and be in alphabetical order. I just need to move the URS and MN accounts to the top of the pile somehow.
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add _
Key:=Range("B2:B" & Lrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:J" & Lrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Here is the part of my macro that deals with this report in full.
Dim Lrow As Long
With ActiveSheet.UsedRange
Lrow = .Cells(.Cells.Count).Row - 4
End With
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add _
Key:=Range("B2:B" & Lrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:J" & Lrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("K").Delete
Columns("A").Delete
Columns("C:F").Delete
Columns("A").ColumnWidth = 12
Columns("B").ColumnWidth = 14.5
Columns("C").ColumnWidth = 24.5
Dim groupMN As Single
Dim groupURS As Single
Dim groupOther As Single
Dim lastMN As Integer
Dim lastURS As Integer
Dim lastOther As Integer
Dim cell As Range
For Each cell In Range("A2:A" & Range("A1").CurrentRegion.Rows.Count)
If Left(cell, 2) = "MN" Then
If ExtractElement(cell, 2, " ") < 200001 Then GoTo BadCCEntry
groupMN = groupMN + cell.Offset(, 4).Value
lastMN = cell.Row
GoTo GoodCCEntry
ElseIf Left(cell, 3) = "URS" Then
If ExtractElement(cell, 2, " ") < 10001 Or ExtractElement(cell, 2, " ") > 200000 Then GoTo BadCCEntry
groupURS = groupURS + cell.Offset(, 4).Value
lastURS = cell.Row
GoTo GoodCCEntry
Else
If Not Val(ExtractElement(cell, 2, " ")) < 9999 Then GoTo BadCCEntry
groupOther = groupOther + cell.Offset(, 4).Value
lastOther = cell.Row
GoTo GoodCCEntry
BadCCEntry:
MsgBox "Account name/number mismatch. Check account numbers and subtotal manually."
GoTo VeryEnd
GoodCCEntry:
End If
Next cell
If groupMN > 0 Then Range("F" & lastMN).Value = groupMN
If groupURS > 0 Then Range("F" & lastURS).Value = groupURS
If groupOther > 0 Then Range("F" & lastOther).Value = groupOther
GoTo VeryEnd
Bookmarks