martinslmn has asked for the wisdom of the Perl Monks concerning the following question:
Can you please help by pointing me in the right direction? Previously I was using manual sorting on Excel 2003 using the following code. It takes a long time to complete. As latest versions of Excel supports Sort by Colour, I would like to move to it. This will give you an idea on what I am looking for.ActiveWorkbook.Worksheets("ami_emr_appointments").Sort.SortFields.Clea +r ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range( _ "A2:A4680"), xlSortOnCellColor, xlAscending, , xlSortNormal).S +ortOnValue.Color _ = RGB(255, 0, 0) ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range( _ "A2:A4680"), xlSortOnCellColor, xlAscending, , xlSortNormal).S +ortOnValue.Color _ = RGB(255, 255, 0) ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range( _ "B2:B4680"), xlSortOnCellColor, xlAscending, , xlSortNormal).S +ortOnValue.Color _ = RGB(255, 255, 0) ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range( _ "C2:C4680"), xlSortOnCellColor, xlAscending, , xlSortNormal).S +ortOnValue.Color _ = RGB(255, 255, 0) ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range( _ "B2:B4680"), xlSortOnCellColor, xlAscending, , xlSortNormal).S +ortOnValue.Color _ = RGB(255, 255, 0) With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("A1:W4680") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With
Thankssub sortByColor() { my $filename = $_[0]; my $no_of_columns = $_[1]; say "Activate $filename"; say "No Of COlumns = $no_of_columns"; $excel->{DisplayAlerts}=0; $excel->Windows($filename)->Activate; $workbook = $excel->Activewindow; $sheet1=$workbook-> Activesheet; my $rows= $sheet1->UsedRange->Rows->{'Count'}; my $cols= $sheet1->UsedRange->Columns->{'Count'}; my $red_count = 0; my $sorted_count = 0; foreach my $row ( 2 .. $rows ) { #next unless defined $sheet1->Cells($row,1)->{'Value'}; my $Range = $sheet1->Range("A$row:A$row"); if ($Range->Interior()->ColorIndex() == 3) { $sheet1->rows($row . ":" . $row)->cut(); my $rngIns=$sheet1->Range(($red_count+2) . ":" . ($red_cou +nt+2)); $rngIns->EntireRow->Insert; $sheet1->rows(($red_count+2) . ":" . ($red_count+2))->Sele +ct(); $sheet1->paste(); $red_count = $red_count + 1; $sorted_count = $sorted_count + 1; } } my $column =""; foreach my $col ( 1 .. $no_of_columns) { $column = colIdToString($col); foreach my $row ( ($sorted_count+2) .. $rows ) { #next unless defined $sheet1->Cells($row,$col)->{'Value'}; my $Range = $sheet1->Range("$column$row:$column$row"); if ($Range->Interior()->ColorIndex() == 6 ) { $sheet1->rows($row . ":" . $row)->cut(); my $rngIns=$sheet1->Range(($sorted_count + 2) . ":" . +($sorted_count+2)); $rngIns->EntireRow->Insert; $sheet1->rows(($sorted_count + 2) . ":" . ($sorted_cou +nt+2))->Select(); $sheet1->paste(); $sorted_count = $sorted_count + 1; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl OLE Excel Sort By Color
by Corion (Patriarch) on Jun 02, 2015 at 12:18 UTC | |
by martinslmn (Novice) on Jun 02, 2015 at 12:43 UTC | |
by hdb (Monsignor) on Jun 02, 2015 at 13:43 UTC | |
by Corion (Patriarch) on Jun 02, 2015 at 13:03 UTC | |
|
Re: Perl OLE Excel Sort By Color
by AnomalousMonk (Archbishop) on Jun 02, 2015 at 13:02 UTC | |
by martinslmn (Novice) on Jun 03, 2015 at 05:41 UTC | |
by hdb (Monsignor) on Jun 03, 2015 at 06:25 UTC | |
by martinslmn (Novice) on Jun 03, 2015 at 09:57 UTC | |
by AnomalousMonk (Archbishop) on Jun 03, 2015 at 12:49 UTC | |
by martinslmn (Novice) on Jun 04, 2015 at 04:07 UTC |