note
davies
<p>As others have implied, Excel may not be the best tool for you. But assuming it is (say you want to send summaries to managers who won't look at anything else), the following code should do the job, <i>subject to a few points</i>:</p>
<p>In [id://998682], you talk about removing Outbound calls, but in the OP you talk about sorting on column F, which contains the call time. As this is N/A in all cases in your example data, I have refrained from sorting. Please let us know if I have misunderstood.</p>
<p>I have used Advanced Filter to do the deletion as it is much faster, but if you want to delete line by line for some other reason, please see [id://952167].</p>
<c>
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
my $filename = 'Z:\Data\Perl\998526\998682.csv';
my $xl = Win32::OLE->new('Excel.Application');
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Open($filename);
my $sht = $wb->Sheets(1);
$sht->Range('A1:A3')->EntireRow->Insert;
$sht->Range('G1')->{Value} = $sht->Range('G4')->{Value};
$sht->Range('G2')->{Value} = 'Outbound';
my $lastcell = $xl->ActiveCell->SpecialCells(xlCellTypeLastCell)->Address;
$sht->Range('A4:' . $lastcell)->AdvancedFilter ({Action => xlFilterInPlace,
CriteriaRange => $sht->Range('G1:G2'),
Unique => 0});
$xl->{DisplayAlerts} = 0;
$sht->Range('A5:' . $lastcell)->Delete;
$xl->{DisplayAlerts} = 1;
$sht->ShowAllData;
$sht->Range('A1:A3')->EntireRow->Delete;
</c>
<p>A few points. I strongly advise against taking control of an existing instance of Excel. I have written a few posts here on the subject. It seems to be a technique widely copied from something I can't remember reading, but if there are many more examples, I'll put up a <strike>rant</strike>meditation as a single point of reference.</p>
<p>When using single quotes, you don't need to use multiple backslashes.</p>
<p>Your technique to find the last row will work for your case, but is very inefficient. I can dream up some cases where it might not work (I haven't tried).</p>
<p>Don't <c>->Select</c> or <c>->Activate</c>. See [id://890933]. It's fine to read these, but changing them is rarely necessary.</p>
<p>Regards,</p>
<p>John Davies</p>
998526
998526