haleybusche
New Member
- Joined
- Mar 7, 2011
- Messages
- 13
I recorded a macro to sort my data in three ways: color, number, and letter. The color column is created using a function GetColor() which I wrote.
This assigns a number to the color in the cell and then I can sort it based on the number. The other two sorts are just standard.
When I run the macro, it doesn't sort correctly. And it doesn't seem to sort the same each time.
Sub Sort() 'Sorting by Color(#) ("AB"), letter ("B), TAT#("A")
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range( _
"AB3:AB189"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B3:B189") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A3:A189") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:AB189")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
My data is dynamic, so the number of rows will change. The number of columns will not. I use A-AB.
I need to sort by AB, A, and then B. Not just the column itself but the associated row as well.
I am not sure, where I am off, but this doesn't seem to be working. Also if there is a more efficient way of doing this I am all ears.
This assigns a number to the color in the cell and then I can sort it based on the number. The other two sorts are just standard.
When I run the macro, it doesn't sort correctly. And it doesn't seem to sort the same each time.
Sub Sort() 'Sorting by Color(#) ("AB"), letter ("B), TAT#("A")
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range( _
"AB3:AB189"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B3:B189") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A3:A189") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:AB189")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
My data is dynamic, so the number of rows will change. The number of columns will not. I use A-AB.
I need to sort by AB, A, and then B. Not just the column itself but the associated row as well.
I am not sure, where I am off, but this doesn't seem to be working. Also if there is a more efficient way of doing this I am all ears.