PC Review


Reply
 
 
=?Utf-8?B?SkhM?=
Guest
Posts: n/a
 
      17th Aug 2007
Hello,
the following code should sort by color. However, if the spreadsheet is
large the macro runs a very long time. Will someone write some code that's
more efficient for large spreadsheets, say over 10K lines?

Thank you.
JHL

Sub ColorSorter()
y = ActiveCell.Column - 1
J = Range("IV1").End(xlToLeft).Column
BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1

Range("A1").Offset(0, J) = "Sort"
For i = 1 To BotRow
Range("A1").Offset(i, J) = Range("A1").Offset(i,
y).Interior.ColorIndex
Next
Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending,
Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Columns(J + 1).Delete
End Sub
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkhM?=
Guest
Posts: n/a
 
      17th Aug 2007
THANK YOU and also for the comments about the speed.

Perhaps I should think of a better index for sorting, then just color code
the result.

"Jim Thomlinson" wrote:

> This is going to take a long time to run no matter what you do... here it is
> with a couple of tweaks but it is going to be a slow process
>
> Sub ColorSorter()
> dim y as long
> dim J as long
> dim BotRow as long
>
> Application.Screenupdating = false
>
> y = ActiveCell.Column - 1
> J = Range("IV1").End(xlToLeft).Column
> BotRow = Cells(rows.count, "A").Offset(0, y).End(xlUp).Row - 1
>
> Range("A1").Offset(0, J) = "Sort"
> For i = 1 To BotRow
> Range("A1").Offset(i, J) = Range("A1").Offset(i,
> y).Interior.ColorIndex
> Next i
> Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending,
> Header:=xlYes, _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> Columns(J + 1).Delete
> Application.Screenupdating = true
>
> End Sub
> --
> HTH...
>
> Jim Thomlinson
>
>
> "JHL" wrote:
>
> > Hello,
> > the following code should sort by color. However, if the spreadsheet is
> > large the macro runs a very long time. Will someone write some code that's
> > more efficient for large spreadsheets, say over 10K lines?
> >
> > Thank you.
> > JHL
> >
> > Sub ColorSorter()
> > y = ActiveCell.Column - 1
> > J = Range("IV1").End(xlToLeft).Column
> > BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1
> >
> > Range("A1").Offset(0, J) = "Sort"
> > For i = 1 To BotRow
> > Range("A1").Offset(i, J) = Range("A1").Offset(i,
> > y).Interior.ColorIndex
> > Next
> > Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending,
> > Header:=xlYes, _
> > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> > Columns(J + 1).Delete
> > End Sub

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      17th Aug 2007
If you incorporate the following modification your colour sort should be
pretty fast, even with 10k rows -

Change -
> For i = 1 To BotRow
> Range("A1").Offset(i, J) = Range("A1").Offset(i,

y).Interior.ColorIndex
> Next


to -

Dim rng As Range, cel As Range
Set rng = Range("A1").Offset(, y).Resize(BotRow, 1)

ReDim arrClrIdx(1 To BotRow, 1 To 1) As Long
For Each cel In rng
i = i + 1
arrClrIdx(i, 1) = cel.Interior.ColorIndex
Next
Range("A1").Offset(, j).Resize(BotRow, 1) = arrClrIdx

I've tried to adapt with your variables and column to sort method, eg your
BotRow doesn't seem right -

> BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1

to
BotRow = Range("A65536").Offset(0, y).End(xlUp).Row

Best double check but hope you get the basic idea.

Also declare your other variables
Dim y As Long, j As Long, i As Long, BotRow As Long

Regards,
Peter T


"JHL" <(E-Mail Removed)> wrote in message
news:1ACC2D00-88CF-48C9-88A6-(E-Mail Removed)...
> Hello,
> the following code should sort by color. However, if the spreadsheet is
> large the macro runs a very long time. Will someone write some code

that's
> more efficient for large spreadsheets, say over 10K lines?
>
> Thank you.
> JHL
>
> Sub ColorSorter()
> y = ActiveCell.Column - 1
> J = Range("IV1").End(xlToLeft).Column
> BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1
>
> Range("A1").Offset(0, J) = "Sort"
> For i = 1 To BotRow
> Range("A1").Offset(i, J) = Range("A1").Offset(i,
> y).Interior.ColorIndex
> Next
> Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending,
> Header:=xlYes, _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> Columns(J + 1).Delete
> End Sub



 
Reply With Quote
 
=?Utf-8?B?SkhM?=
Guest
Posts: n/a
 
      20th Aug 2007
Peter T
WOW is all I can say. These mods made it lightin' FAST!!!

Thanks

"Peter T" wrote:

> If you incorporate the following modification your colour sort should be
> pretty fast, even with 10k rows -
>
> Change -
> > For i = 1 To BotRow
> > Range("A1").Offset(i, J) = Range("A1").Offset(i,

> y).Interior.ColorIndex
> > Next

>
> to -
>
> Dim rng As Range, cel As Range
> Set rng = Range("A1").Offset(, y).Resize(BotRow, 1)
>
> ReDim arrClrIdx(1 To BotRow, 1 To 1) As Long
> For Each cel In rng
> i = i + 1
> arrClrIdx(i, 1) = cel.Interior.ColorIndex
> Next
> Range("A1").Offset(, j).Resize(BotRow, 1) = arrClrIdx
>
> I've tried to adapt with your variables and column to sort method, eg your
> BotRow doesn't seem right -
>
> > BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1

> to
> BotRow = Range("A65536").Offset(0, y).End(xlUp).Row
>
> Best double check but hope you get the basic idea.
>
> Also declare your other variables
> Dim y As Long, j As Long, i As Long, BotRow As Long
>
> Regards,
> Peter T
>
>
> "JHL" <(E-Mail Removed)> wrote in message
> news:1ACC2D00-88CF-48C9-88A6-(E-Mail Removed)...
> > Hello,
> > the following code should sort by color. However, if the spreadsheet is
> > large the macro runs a very long time. Will someone write some code

> that's
> > more efficient for large spreadsheets, say over 10K lines?
> >
> > Thank you.
> > JHL
> >
> > Sub ColorSorter()
> > y = ActiveCell.Column - 1
> > J = Range("IV1").End(xlToLeft).Column
> > BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1
> >
> > Range("A1").Offset(0, J) = "Sort"
> > For i = 1 To BotRow
> > Range("A1").Offset(i, J) = Range("A1").Offset(i,
> > y).Interior.ColorIndex
> > Next
> > Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending,
> > Header:=xlYes, _
> > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> > Columns(J + 1).Delete
> > End Sub

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort by color: Is there an easy way to sort columns or rows in EX MGP Microsoft Excel Worksheet Functions 5 16th Aug 2008 11:28 AM
How do I sort my data by color? (color applied to rows) =?Utf-8?B?VFRvd25zZW5k?= Microsoft Excel Worksheet Functions 0 7th Sep 2006 09:09 PM
Sort or sub-total by Fill color or font color =?Utf-8?B?RXhjZWxfc2Vla19oZWxw?= Microsoft Excel Misc 1 27th Apr 2006 09:01 PM
Excel sort by Fill Color by custom list sort =?Utf-8?B?RGFzaDRDYXNo?= Microsoft Excel Misc 2 29th Jul 2005 10:45 PM
Sort Column by Text Color or BackGrd Color Ken Dickens Microsoft Excel Misc 5 15th Jan 2004 04:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:19 PM.