I have a script that loops through a workbook and copies all tabs to a new workbook. For some reason, though, the first tab is always being skipped.
for ($i=1; $i -lt $myworkbook.Worksheets.Count; $i++) {
... other code
$SheetToCopy = $source.Worksheets.Items($i)
$sheetToCopy.Copy($target)
}
- I've tried setting
$ito 0, but then powershell complains that zero is not valid. - Tried using
$workbook.sheets.itemand$workbook.worksheets.item, but they seem to do the same thing
Can anyone shed some light on what is going on here?
Additional Information:
The workbooks will never have the same named tabs, for this reason i'm using index rather than name.
(i = 1; i <= worksheets.Count; i++), otherwise when it hits the last sheet with an index of say 4 and i is at 4, it's never going to copy it. Unless that's what you intended.<, but in this case i need to use<=