I have created a datasort function in vba. When I step through the code it seems to be working correctly except the sort is not sorting correctly. Any help would be appreciated.
Tom
Tom
Code:
Dim a As Range
Dim c As Range
Dim d As Range
Dim e As Range
For Each a In Worksheets("ClientProcessing").Range("E8:E95")
If a > 0 Then
'3 key sort
Selection.Sort Key1:=Range("E8"), Order1:=xlAscending, Key2:=Range("A8") _
, Order2:=xlAscending, Key3:=Range("J8"), Order3:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
Else
'2 key sort
Selection.Sort Key1:=Range("C8"), Order1:=xlAscending, Key2:=Range("A8") _
, Order2:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
End If
Next
[\code]
So stepping through the code this is what happens. In cell E8 there is a 1. So the code goes into the following code:
Selection.Sort Key1:=Range("E8"), Order1:=xlAscending, Key2:=Range("A8") _
, Order2:=xlAscending, Key3:=Range("J8"), Order3:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
Which is what I want it to do. When I look at range E8 I have a 1 which is correct.
When I look at range A8 I have a SPN which is correct. In cell J8 I have a Y which is correct.
When the sort gets execucted the header data in row 7 gets moves to row 42. The freeze pane in row 7 moves down to row 8. The data in rows 101 and row 102 gets inserted into rows 42 and 43. The data in rows 98, 99 and 100 get inserted into rows 70,71 and 72.