A quick first impression: your approach is never going to work. In your ### Do some stuff block, you are inserting a row. This will create the same problem as deleting a row, something I have analysed in RFC Tutorial - Deleting Excel Rows, Columns and Sheets. Start at the end and work backwards and you will eliminate at least one of your problems.

Almost immediate update: the rows are numbered sequentially in every implementation of Excel (and every other spreadsheet - dozens) I have seen. However, rows and columns may be hidden. If they are hidden, that is from the human eye, not VBA or Perl. Your code will go from row 1 to row 2 regardless of whether row 2 is visible to you.

Update 2: some code. Update 3 changed the code to include a demonstration of testing whether a row is hidden.

use strict; use warnings; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; # Make sure there is precisely 1 sheet for my $nSht (2..$wb->Sheets->{Count}) { $wb->Sheets(2)->Delete; } # Put some data in my $sht = $wb->Sheets(1); $sht->Cells(1, 1)->{Value} = "A"; $sht->Cells(2, 1)->{Value} = "B"; $sht->Cells(3, 1)->{Value} = "C"; # Hide row 2 $sht->Cells(2, 1)->EntireRow->{Hidden} = 1; # Process 3 rows, demonstrating a bug for my $row (1..3) { if ("A" ne $sht->Cells($row, 1)->{Value}) { $sht->Cells($row + 1, 1)->EntireRow->Insert; $sht->Cells($row, 1)->EntireRow->Copy($sht->Cells($row + 1, 1) +); } } use feature 'say'; for my $row (1..5) { if ($sht->Cells($row, 1)->EntireRow->{Hidden}) { say "Row $row is hidden"; } }

Run this code and you will see three hidden rows. This is because row 2 (B) has been copied to row 3. After that, row 3 (a copy of B) has been copied to row 4. This is the loop error bug I mention in my post about deleting rows.

To reveal the rows, select rows 1 to 5, the two visible rows and the three hidden ones. Doing this with the mouse can be awkward, but I always prefer the keyboard anyway. Alt-H (Home), O (Format), U (Hide & Unhide), O (Unhide Rows) will show the three copies of what was row 2. If you are using a version with the old menu tree, it's Alt-O, R, U.

Regards,

John Davies


In reply to Re: Win32::OLE and Excel: Iterating when certain row numbers are missing by davies
in thread Win32::OLE and Excel: Iterating when certain row numbers are missing by ownlifeful

Title:
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, details, 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, summary, 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.