LoginSignup
0
0

More than 3 years have passed since last update.

オートフィルタで抽出した行を削除しデータをスリム化する方法

Posted at

今回の目的は
何十万行と存在する大量のデータから必要なデータだけを残して
不要なデータを削除する方法。

ちなみにこの大量のデータは1ファイルではなく、複数のExcelファイルに
分かれているため、単一のファイル処理だけでは追いつかない。

今回行ったことは下記の通り。
1.不要なデータを抽出する指標のデータを用意
2.オートフィルタ機能を利用して不要なデータを抽出
3.不要なデータが入力された行を全て削除
4.オートフィルタを解除し必要なデータだけが残る。
上記1〜4の処理をフォルダ内の全てのExcelファイルに対して行う。
(繰り返し処理)

Sub Sample()
Dim LastRow As Long
Dim LastColumn As Long

' 指標の列を作成
Worksheets("Sheet1").Activate
LastRow = Range("A" & Rows.Count).End(xlUp).Row '上方向に最終行を検索する
LastColumn = Cells(7, Columns.Count).End(xlToLeft).Column + 1 '左方向に最終列+1を検索する
Cells(5, LastColumn).Value = "指標"
Cells(6, LastColumn).Value = "単位"
Cells(7, LastColumn).Value = "=ABS(RC[-3]-RC[-1])"
Range("J7").AutoFill _
Destination:=Range(Cells(7, LastColumn), Cells(LastRow, LastColumn))

' オートフィルタで抽出した行を削除
Range(Cells(6, 1), Cells(6, LastColumn)).AutoFilter LastColumn, ">2.9"
With Range("A6").CurrentRegion.Offset(5, 0)
.Resize(.Rows.Count - 1).EntireRow.Delete
End With
ActiveSheet.ShowAllData

End Sub

以上です。

0
0
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
0