Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Sort not working properly

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
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

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.
 
Access? Excel?

If Excel, you ought to be posting in forum707.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
how about [red]xlYes[/red]
Code:
Selection.Sort Key1:=Range("E8"), Order1:=xlAscending, Key2:=Range("A8") _
, Order2:=xlAscending, Key3:=Range("J8"), Order3:=xlAscending, _
Header:=xl[b][red]No[/red][/b], OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,
Sorry I posted to the wrong forum. I will repost. Changing the xlno to xlyes did fix the header from getting moved.

Thanks
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top