Hi, everyone
I'm trying to automate a sort function using VBA. I'm looking at student scores and I'm trying to create class groups based on scores while keeping ages within 4 years.
Here's the spreadsheet: Sort range.xlsm
It's a link to Google Drive as I'm using a work computer:
I have managed to create the initial sort in this order: Grade 4, Grade 3, Grade 2, Grade 1. The macro includes whole columns as I want to be able to use it with different data sets:
Sub Sort()
Columns("A:H").Select
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("H:H") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("G:G") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("F:F") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("E:E") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("A:H")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
From here, I want to find the lowest number in column H and find the next half number in column H, e.g. if the first number in column H is "0.5" the next number would be "1.". In this instance, the lowest number is "0" and the next number is "0.5".
With the data range A2:H10 (everyone in this range has a score in column H of "0" to "0.5"), I would like to sort this data by: Age, Grade 4, Grade 3, Grade 2, and Grade 1.
From then I want to repeat the process above but starting from the next row. In this instance it's students with a score in column H with "1" to "1.5". This would be range A11:A24. Again, I would like to sort this data by: Age, Grade 4, Grade 3, Grade 2, and Grade 1.
I want it to repeat this process through the rest of the data, sorting by Age, Grade 4, Grade 3, Grade 2, and Grade 1.
Can anyone help?
Thank you in advance.
I'm trying to automate a sort function using VBA. I'm looking at student scores and I'm trying to create class groups based on scores while keeping ages within 4 years.
Here's the spreadsheet: Sort range.xlsm
It's a link to Google Drive as I'm using a work computer:
I have managed to create the initial sort in this order: Grade 4, Grade 3, Grade 2, Grade 1. The macro includes whole columns as I want to be able to use it with different data sets:
Sub Sort()
Columns("A:H").Select
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("H:H") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("G:G") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("F:F") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("E:E") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("A:H")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
From here, I want to find the lowest number in column H and find the next half number in column H, e.g. if the first number in column H is "0.5" the next number would be "1.". In this instance, the lowest number is "0" and the next number is "0.5".
With the data range A2:H10 (everyone in this range has a score in column H of "0" to "0.5"), I would like to sort this data by: Age, Grade 4, Grade 3, Grade 2, and Grade 1.
From then I want to repeat the process above but starting from the next row. In this instance it's students with a score in column H with "1" to "1.5". This would be range A11:A24. Again, I would like to sort this data by: Age, Grade 4, Grade 3, Grade 2, and Grade 1.
I want it to repeat this process through the rest of the data, sorting by Age, Grade 4, Grade 3, Grade 2, and Grade 1.
Can anyone help?
Thank you in advance.