in reply to How to operate on multiple Excel worksheets with variable array

Excel maintains all these arrays for you. You don't have to do the work yourself, whether in Perl or anywhere else.

use strict; use warnings; use Win32::OLE; my $excel = Win32::OLE -> new("Excel.Application"); $excel->{Visible} = 1; $excel->{DisplayAlerts}=0; my $oldsheets = $excel->{SheetsInNewWorkbook}; $excel->{SheetsInNewWorkbook} = 5; my $wb = $excel->Workbooks->Add; $excel->{SheetsInNewWorkbook} = $oldsheets; for my $i (1..$wb->Sheets->{Count}) { $wb->Sheets($i)->Cells(1, 1)->{Value} = "Sheet $i"; }

I think the last 3 lines are the equivalent of your matlab code.

Note, however, the work I have done on sheet numbers. I prefer not to use SheetsInNewWorkbook as it gets saved whenever Excel closes normally. You might have some users who have bothered to set up Excel sensibly. They won't take kindly to you changing their default number of sheets. And if your Perl crashes with an Excel instance open, closing that instance will save the current setting, which is why I prefer not to risk it. But that's just my personal paranoia. The saving & restoring I do in my code should be fine.

Regards,

John Davies

  • Comment on Re: How to operate on multiple Excel worksheets with variable array
  • Download Code

Replies are listed 'Best First'.
Re^2: How to operate on multiple Excel worksheets with variable array
by bill5262 (Acolyte) on Aug 09, 2016 at 07:36 UTC

    Thank you so much.