(1日遅れて1ドル足りない)
私はまだExcel 2003を使用していますが、この問題があります。これらのマクロは古いシートを削除し、新しいシートを追加します。最初のマクロはシートを切り捨てて、「Ctl-End Lastcell」がコンテンツの最後になるようにします。2 番目のマクロは、シートを削除し、同じ名前の新しいシートを追加するだけなので、「Ctl-End Lastcell」は A1 になります。
Sub ShTrunc() ' truncate active sheet to content only
Dim sold$, snew$, rowz&, colz&, zRange As Range
' -- get old and new sheet names
sold = ActiveSheet.Name ' old sheet name
Sheets.Add
snew = ActiveSheet.Name ' new name
' -- get the "true" last row and column
' based on http://www.rondebruin.nl/win/s9/win005.htm
Sheets(sold).Activate
Set zRange = Cells.Find("*", [a1], xlFormulas, xlPart, xlByRows, xlPrevious, False)
If zRange Is Nothing Then rowz = 1 Else rowz = zRange.Row
Set zRange = Cells.Find("*", [a1], xlFormulas, xlPart, xlByColumns, xlPrevious, False)
If zRange Is Nothing Then colz = 1 Else colz = zRange.Column
' -- copy the content from old sheet, paste to new sheet
Range(Cells(1, 1), Cells(rowz, colz)).Copy ' Sheets(snew).Cells(1, 1)
Sheets(snew).Activate
ActiveSheet.Paste
' -- delete old sheet and rename new to old
Application.DisplayAlerts = False
Sheets(sold).Delete
Application.DisplayAlerts = True
Sheets(snew).Name = sold ' rename to old name
' -- the following checks if the world works as it should
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row <> rowz Then Stop
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column <> colz Then Stop
End Sub
Sub ShDelAdd() ' delete and add active sheet (to CLEAR it)
' this is a simpler version of ShTrunc if you only want to clear a sheet
Dim sold$, snew$
sold = ActiveSheet.Name
Application.DisplayAlerts = False
Sheets(sold).Delete
Application.DisplayAlerts = True
Sheets.Add
snew = ActiveSheet.Name ' new name
Sheets(snew).Name = sold ' rename to old name
' -- the following checks if the world works as it should
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row <> 1 Then Stop
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column <> 1 Then Stop
End Sub