Help us understand the problem. What is going on with this article?

Excel の WorkSheets を PowerShell で扱うと楽になる

VBA だと worksheets を走査するのにいちいち For したりするので面倒ですよね?

PowerShell なら1行でヤレます。

$Excel = New-Object -com Excel.Application
$Book = $Excel.WorkBooks.Open($BookPath)
$Book.WorkSheets | ? { $_.Name -match "Sheet[0-9]*" } | % { $_.Delete() } # 1行!

ただし、広い範囲の Range とかだと読み出しに時間がかかるので遅くなります。

$Excel = New-Object -com Excel.Application
$Book = $Excel.WorkBooks.Open($BookPath)
$Sheet = $Book.WorkSheets | Select -First 1
$Sheet.Range("A1:Z1000") | ? { $_.Text } | measure # 遅い

基本的に PowerShell の方が構文が綺麗なので使いたいんですが、VBA の方が実行速度が出やすいんですよね。


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away