▽1
●
Mook ●300ポイント ベストアンサー |
通常であれば下記で出来ると思いますが、
Option Explicit Sub DeleteEmptyAreaSimple() Dim ws As Worksheet Dim lastRow As Long Dim lastCol As Long For Each ws In Worksheets With ws lastRow = ws.UsedRange.Rows.Count lastCol = ws.UsedRange.Columns.Count If lastCol < Columns.Count Then .Range(.Cells(1, lastCol + 1), .Cells(1, Columns.Count)).EntireColumn.Delete End If If lastRow < Rows.Count Then .Range(.Cells(lastRow + 1, "A"), .Cells(Rows.Count, "A")).EntireRow.Delete End If End With Next End Sub
シートがデータのない範囲まで、使用している範囲と認識していることも有るので、
実際のデータを確認して範囲を削除する例です。
Option Explicit Sub DeleteEmptyArea() Dim ws As Worksheet For Each ws In Worksheets cleanupWS ws Next End Sub Sub cleanupWS(ws As Worksheet) Dim lastRow As Long Dim maxLastRow As Long Dim maxLastCol As Long With ws maxLastRow = .Cells(Rows.Count, "A").End(xlUp).Row maxLastCol = 1 Dim c As Long For c = 2 To Columns.Count If Application.WorksheetFunction.CountA(.Columns(c)) <> 0 Then maxLastCol = c lastRow = .Cells(Rows.Count, c).End(xlUp).Row If lastRow > maxLastRow Then maxLastRow = lastRow Next If maxLastCol < Columns.Count Then .Range(.Cells(1, maxLastCol + 1), .Cells(1, Columns.Count)).EntireColumn.Delete End If If maxLastRow < Rows.Count Then .Range(.Cells(maxLastRow + 1, "A"), .Cells(Rows.Count, "A")).EntireRow.Delete End If End With End Sub