Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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, subject to a few points:

In Re^2: Sort CSV file within Excel based on specific column, 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.

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 RFC Tutorial - Deleting Excel Rows, Columns and Sheets.

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)->Addr +ess; $sht->Range('A4:' . $lastcell)->AdvancedFilter ({Action => xlFi +lterInPlace, 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;

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 rantmeditation as a single point of reference.

When using single quotes, you don't need to use multiple backslashes.

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).

Don't ->Select or ->Activate. See Excel’s Select and Activate considered harmful. It's fine to read these, but changing them is rarely necessary.


John Davies

In reply to Re: Sort CSV file within Excel based on specific column by davies
in thread Sort CSV file within Excel based on specific column by jaacmmason

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2015-12-01 02:08 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (791 votes), past polls