Hi Marco
Sorry for the delay in response - I have been busy with other things.
I ran your code in both XL2003 and XL2007 only on a sample of 1000
lines, those being a repeat of the 2 lines of data supplied.
Both ran to completion, and although I didn't time them I believe
XL2007 was faster.
I wonder whether you have a lot of Conditional Formatting in your
file. I had none.
I have read that some people have experienced very slow response in
files in XL2007 when there is a lot of CF applied.
However, this time round I did take the time to read through what your
code was doing.
As you are looping through each row 5 times as you do your block of 7
column sorts left to right, with 5000 lines this is going to take a
lot of time.
I have added some code as below.
First I have numbered the rows as they are.
Then calculated the length of each row.
Sorted by row length
Looped through the groups of same length carrying out your 7 column
sort
Sorted the whole range back in the original row order.
So rather than 1000 iterations x 5, my routine had 2 x 5.
Yours will be more than 2 x 5, as you will have more row lengths, but
it cannot be more than 38 x 5 as there are only 38 columns at most.
Anyway, it will be a minimum of 25 times faster and probably quite a
lot more.
I am sure there are more efficient ways of writing the code than I am
showing her, but it may help to get you running faster.
Sub OrderMany2()
Dim objSheet As Excel.Worksheet
Dim iLastRow As Long, iLastColumn As Long
Dim i As Long, c As Long
Dim j As Long
Dim begrange As Long, endrange As Long, endrange2 As Range
Set objSheet = ActiveWorkbook.Worksheets(1)
iLastRow = objSheet.Cells(objSheet.Rows.Count, "c").End(xlUp).Row
Cells.Select
Cells.Find(What:="*", AFTER:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious,
_
MatchCase:=False, SearchFormat:=False).Activate
iLastColumn = ActiveCell.Column
c = iLastColumn + 12
objSheet.Cells(2, c).FormulaR1C1 = "1"
objSheet.Cells(3, c).FormulaR1C1 = "2"
Range(Cells(2, c), Cells(3, c)).Select
Selection.AutoFill Destination:=Range(Cells(2, c), Cells(iLastRow,
c)), Type:=xlFillDefault
Cells(2, c + 1).FormulaR1C1 = "=COUNT(RC[-37]:RC[-13])"
Cells(2, c + 1).Select
Selection.AutoFill Destination:=Range(Cells(2, c + 1),
Cells(iLastRow, c + 1))
Cells(2, c + 2).FormulaR1C1 = "=IF(RC[-1]=R[-1]C[-1],"""",RC[-1])"
Cells(2, c + 2).Select
Selection.AutoFill Destination:=Range(Cells(2, c + 2),
Cells(iLastRow, c + 2))
Cells(2, c + 3).FormulaR1C1 = "=COUNT(C[-1]) -1"
objSheet.Range(objSheet.Cells(2, 2), objSheet.Cells(2,
2).Offset(iLastRow, c)).Sort _
Key1:=objSheet.Range(objSheet.Cells(2, c + 1), _
objSheet.Cells(2, 2).Offset(iLastRow, c)), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Cells(iLastRow, c + 2) = iLastRow
begrange = 0: endrange = 2:
Set endrange2 = Cells(2, c + 2)
For i = 1 To Cells(2, c + 3).Value
begrange = begrange + endrange - 1
With Range(Cells(2, c + 2), Cells(iLastRow, c + 2))
Set endrange2 = .Find(What:="*", AFTER:=endrange2, _
LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False)
End With
endrange = endrange2.Row
endrange = endrange - begrange + 1
For j = 3 To 31 Step 7
objSheet.Range(objSheet.Cells(begrange, j), objSheet.Cells(begrange,
j).Offset(endrange, 6)).Select
objSheet.Range(objSheet.Cells(begrange, j), objSheet.Cells(begrange,
j).Offset(endrange, 6)).Sort _
Key1:=objSheet.Range(objSheet.Cells(begrange, j), _
objSheet.Cells(begrange, j).Offset(endrange, 6)), Order1:=xlAscending,
_
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
Next j
Next i
objSheet.Range(objSheet.Cells(2, 2), objSheet.Cells(2,
2).Offset(iLastRow, c)).Sort _
Key1:=objSheet.Range(objSheet.Cells(2, c), _
objSheet.Cells(2, 2).Offset(iLastRow, c)), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range(Cells(2, c), Cells(iLastRow, c + 4)).Clear
Cells(2, 3).Select
MsgBox "Sort complete.", vbInformation
End Sub
--
Regards
Roger Govier
Marco said:
"Roger Govier" wrote
Hi Roger
How can i adapt the code with new feature?, i am not expert... ????
this is a sample value data
76 87 58 38 26 42 83 22 64 88 44 78 66 68 45 42 17 27 51 21 34 77 79
89 75 86 55 81 9 15 27 43 62 31 13
When I say that the code over does not work, it is not exact if I try
with 10 lines I work but if I try with 5000 rows in 2003 it puts 15
second with 2007 I have not never seen the end also after 10 minute.
Can you try, copy the values over for 5000 row is not important if
the lines are equal .
Regards
Sub OrderMany()
Dim objSheet As Excel.Worksheet
Dim iLastRow As Long
Dim i As Long
Dim j As Long
Set objSheet = ActiveWorkbook.Worksheets(1)
iLastRow = objSheet.Cells(objSheet.Rows.Count, "C").End(xlUp).Row
For i = 2 To iLastRow
For j = 3 To 31 Step 7
objSheet.Range(objSheet.Cells(i, j), objSheet.Cells(i, j).Offset(0,
6)).Sort _
Key1:=objSheet.Range(objSheet.Cells(i, j), _
objSheet.Cells(i, j).Offset(0, 6)), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
Next j
Next i
MsgBox "Sort complete.", vbInformation
End Sub