0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel VBA 最終行の下に非表示の行が存在する場合に削除するマクロ

Posted at

通常の手筋

ふつうはLastRowから行きます。
適当に書くと

Dim iRow , LastRow
LastRow = ActiveSheet.UsedRange.Rows.Count
For iRow = LastRow To 1 Step -1
If Rows(iRow).Hidden Then Rows(iRow).Delete
Next

しかし

最終行の下にさらに隠し行がある場合、単純に削除ができない場合があることが発生

そこで

ubara先生のサイトを参考に作ったのが以下のもの

Dim iRow As Long
Activesheet.Activate ' 目的のシートを表示させる(つまり選択する)
For iRow = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row To Cells.SpecialCells(xlCellTypeLastCell).Row Step -1
If Rows(iRow).Hidden Then Rows(iRow).Delete
Next

Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Rowでとにかく何らかの入力がある最終セルの行数を取得。これは隠し行でもカウントする...(1)
Cells.SpecialCells(xlCellTypeLastCell).Rowは表示されているセルの最終行をとる...(2)
(1)が(2)より大きい(つまり必ず隠し行が発生する)場合(2)から(1)まで減算し、隠し行なら削除する。
また、この方式は1シートに表が複数あるとうまくいかない可能性があります。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?