Quickest way to clear all sheet contents VBA

前端 未结 4 1989
星月不相逢
星月不相逢 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:30

    The .Cells range isn't limited to ones that are being used, so your code is clearing the content of 1,048,576 rows and 16,384 columns - 17,179,869,184 total cells. That's going to take a while. Just clear the UsedRange instead:

    Sheets("Zeros").UsedRange.ClearContents
    

    Alternately, you can delete the sheet and re-add it:

    Application.DisplayAlerts = False
    Sheets("Zeros").Delete
    Application.DisplayAlerts = True
    Dim sheet As Worksheet
    Set sheet = Sheets.Add
    sheet.Name = "Zeros"
    

提交回复
热议问题