Quickest way to clear all sheet contents VBA

前端 未结 4 1987
星月不相逢
星月不相逢 2021-02-03 18:05

I have a large sheet that I need to delete all the contents of. When I try to simply clear it without VBA it goes into not responding mode. When using a macro such as:



        
4条回答
  •  别那么骄傲
    2021-02-03 18:21

    Technically, and from Comintern's accepted workaround, I believe you actually want to Delete all the Cells in the Sheet. Which removes Formatting (See footnote for exceptions), etc. as well as the Cells Contents. I.e. Sheets("Zeroes").Cells.Delete

    Combined also with UsedRange, ScreenUpdating and Calculation skipping it should be nearly intantaneous:

    Sub DeleteCells ()
        Application.Calculation = XlManual
        Application.ScreenUpdating = False
        Sheets("Zeroes").UsedRange.Delete
        Application.ScreenUpdating = True
        Application.Calculation = xlAutomatic
    End Sub
    

    Or if you prefer to respect the Calculation State Excel is currently in:

    Sub DeleteCells ()
        Dim SaveCalcState
        SaveCalcState = Application.Calculation
        Application.Calculation = XlManual
        Application.ScreenUpdating = False
        Sheets("Zeroes").UsedRange.Delete
        Application.ScreenUpdating = True
        Application.Calculation = SaveCalcState
    End Sub
    

    Footnote: If formatting was applied for an Entire Column, then it is not deleted. This includes Font Colour, Fill Colour and Borders, the Format Category (like General, Date, Text, Etc.) and perhaps other properties too, but

    Conditional formatting IS deleted, as is Entire Row formatting.

    (Entire Column formatting is quite useful if you are importing raw data repeatedly to a sheet as it will conform to the Formats originally applied if a simple Paste-Values-Only type import is done.)

提交回复
热议问题