Excel sheet clear all

Excel for Microsoft 365 Excel 2021 Excel 2019 Excel 2016 Excel 2013 Excel 2010 Excel 2007 More…Less

You can clear cells to remove the cell contents (formulas and data), formats (including number formats, conditional formats, and borders), and any attached comments. The cleared cells remain as blank or unformatted cells on the worksheet.

  1. Select the cells, rows, or columns that you want to clear.

    Tip: To cancel a selection of cells, click any cell on the worksheet.

  2. On the Home tab, in the Editing group, click the arrow next to the Clear button Button image, and then do one of the following:

    • To clear all contents, formats, and comments that are contained in the selected cells, click Clear All.

    • To clear only the formats that are applied to the selected cells, click Clear Formats.

    • To clear only the contents in the selected cells, leaving any formats and comments in place, click Clear Contents.

    • To clear any comments or notes that are attached to the selected cells, click Clear Comments and Notes.

    • To clear any hyperlinks that are attached to the selected cells, select Clear Hyperlinks.

Notes: 

  • If you click a cell and then press DELETE or BACKSPACE, you clear the cell contents without removing any cell formats or cell comments.

  • If you clear a cell by using Clear All or Clear Contents, the cell no longer contains a value, and a formula that refers to that cell receives a value of 0 (zero).

  • If you want to remove cells from the worksheet and shift the surrounding cells to fill the space, you can select the cells and delete them. On the Home tab, in the Cells group, click the arrow next to Delete, and then click Delete Cells.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Return to VBA Code Examples

In this Article

  • Clear ActiveSheet
    • Clear Everything (Contents, Formats, Comments, etc.)
    • Clear Contents
    • Clear Formats
    • Delete Worksheet UsedRange
  • Clear Sheet (By Name)
  • Clear Worksheet (From Variable)

In VBA it’s fast and easy to clear an entire sheet (or worksheet).

Clear ActiveSheet

Clear Everything (Contents, Formats, Comments, etc.)

This will clear the Activesheet’s cells of all cell properties: contents, formats, comments, etc:

Cells.Clear

Clear Contents

Instead, you can clear ONLY the cell contents:

Cells.ClearContents

Clear Formats

or only the Cell Formats:

Cells.ClearFormats

By typing: Cells.Clear into the VBA Editor you can see the list of Clear methods available to you:

vba clear entire sheet

Delete Worksheet UsedRange

You can also delete the entire worksheet’s UsedRange. This can also delete objects (shapes, charts, textboxes).

ActiveSheet.UsedRange.Delete

Clear Sheet (By Name)

To clear a specific sheet use the following code where “Sheet1” is the name of the sheet to clear:

Sheets("Sheet1").Cells.Clear

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

automacro

Learn More

Clear Worksheet (From Variable)

To clear a sheet defined by an object variable use the following code:

dim ws as worksheet

Set ws = Sheets("Sheet1")

ws.Cells.Clear

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.)

Home / VBA / How to CLEAR an Entire Sheet using VBA in Excel

To clear an entire sheet using a VBA code in one go, you need to use two things, first is the CLEAR method, and then CELLS to refer to all the cells of a worksheet.

Clear the Activesheet

Let’s say if you want to clear all the content from the activate sheet, the code would be like the below.

Cells.Clear

When you run this code, it will clear everything from all the cells from the active sheet, as I have already mentioned that we have used clear to refer to the entire worksheet and clear to clear it.

Clear a Specific Sheet

There’s one thing that you need to note down that to clear a sheet that sheet needs to be activated. So let’s say you want to clear the “Sheet1”, the code would be like:

Sheets("Sheet1").Activate
Cells.Clear

In this code, the first activates the “Sheet1” and the second line clears everything.

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Other Things to Clear

You can also use the below methods to clear different things.

Cells.ClearContents 'to clear contents
Cells.ClearComments 'to clear Comments
Cells.ClearFormats 'to clears formatting
Cells.ClearHyperlinks 'to clear hyperlinks
Cells.ClearNotes 'to clear notes
Cells.ClearOutline 'to clears outline

Clear a Sheet from a Specific Workbook

The below code can refer to the workbook “Book1” and clear the sheet “Sheet1”. But make sure to have the workbook open at the time run this code.

Workbooks("Book1").Sheets("Sheet1").Activate
Cells.Clear

This code first activates the “Sheet1” from the book and clears it.

Clear a Sheet from a Workbook that is Closed

And in the below code, we have referred to the “Sheet1” from the workbook “sample-file”, stored in a specific location.

Sub vba_clear_sheet()

Dim wb As Workbook

Application.ScreenUpdating = False

Set wb = Workbooks.Open("C:UsersDellDesktopsample-file.xlsx")
wb.Sheets("Sheet1").Activate
Cells.Clear
wb.Close SaveChanges:=True

Application.ScreenUpdating = False

End Sub

More Tutorials on VBA Worksheets

  • Back to VBA Worksheet / VBA Tutorial

Содержание

  1. VBA Clear Entire Sheet
  2. Clear ActiveSheet
  3. Clear Everything (Contents, Formats, Comments, etc.)
  4. Clear Contents
  5. Clear Formats
  6. Delete Worksheet UsedRange
  7. Clear Sheet (By Name)
  8. VBA Coding Made Easy
  9. Clear Worksheet (From Variable)
  10. VBA Code Examples Add-in
  11. How to use VBA to clear entire sheet in Excel
  12. Introduction
  13. Excel VBA to clear entire sheet
  14. Example
  15. Alternative methods
  16. Summary
  17. Excel-VBA Solutions
  18. Pages
  19. Clear Sheet Using VBA
  20. VBA Delete or Clear Worksheet
  21. Delete Worksheet
  22. Delete Worksheet Without Prompt
  23. Delete Sheet If It Exists
  24. Clear Sheet
  25. Clear Sheet Contents
  26. VBA Coding Made Easy
  27. Clear Sheet UsedRange
  28. VBA Code Examples Add-in
  29. Quickest way to clear all sheet contents VBA
  30. 4 Answers 4

VBA Clear Entire Sheet

In this Article

In VBA it’s fast and easy to clear an entire sheet (or worksheet).

Clear ActiveSheet

Clear Everything (Contents, Formats, Comments, etc.)

This will clear the Activesheet’s cells of all cell properties: contents, formats, comments, etc:

Clear Contents

Clear Formats

or only the Cell Formats:

By typing: Cells.Clear into the VBA Editor you can see the list of Clear methods available to you:

Delete Worksheet UsedRange

You can also delete the entire worksheet’s UsedRange. This can also delete objects (shapes, charts, textboxes).

Clear Sheet (By Name)

To clear a specific sheet use the following code where “Sheet1” is the name of the sheet to clear:

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

Clear Worksheet (From Variable)

To clear a sheet defined by an object variable use the following code:

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

Источник

How to use VBA to clear entire sheet in Excel

With Excel VBA you can achieve almost anything but lets start with the basics. This post will show you how to clear the entire worksheet with Excel VBA, this is useful for clearing out those large datasets when you no longer need them and saving on some space.

Introduction

With Excel VBA it is possible to clear the entire worksheet so leaving you with a nice blank canvas. This has many practical applications in the business world but one of the main reasons you might want to clear the entire sheet is when you no longer need large, memory hogging datasets.

For example you might have some VBA script that imports a raw data file then builds a summary table or dashboard. When those tables, charts and dashboards have been completed you might want to clear the data behind it, possibly for data privacy reasons or just to reduce the end file size to make it email friendly.

Excel VBA to clear entire sheet

The VBA for clearing the entire sheet can be written in a few ways but common practice is to use the line of code that reads:

Example

Imagine you have a worksheet full of data that looks something like this:

To clear the data on Sheet1 using Excel VBA simply open the VBA code window (ALT + F11) and insert the code:

In Excel this should look something like this:

When you run that code it will clear the entire contents of Sheet1 leaving you with a nice blank canvas:

Alternative methods

The clear contents method shown above is commonly used but sometimes you might have additional formatting on a worksheet and you want to clear that along with the data. For example you may have cell shadings, borders or conditional formatting.

To clear the entire sheet from everything, including all the formatting, you need to make a small change to the Excel VBA code, removing the clear contents command and replacing it with the delete command like so:

Finally there is one more alternative and that is to remove the entire worksheet from the Excel file.

Again this requires a minor change to the code where instead of specifying to clear the contents or delete the cells on the worksheet we just delete the whole worksheet:

Note that if you use this method there will no longer be a Sheet1 in the Excel file.

Summary

Excel VBA to clear an entire sheet can be achieved in three ways:

  1. Use the ClearContents method when you want to clear raw data from a worksheet.
  2. Use the Cells.Delete method when you have additional formatting on the worksheet to clear.
  3. Use the Delete method when you want to get rid of the entire worksheet from your Excel file.

If you would like to learn more VBA and unlock some of Excels most powerful features please check out some of the other posts on this site. Alternatively a great learning resource for Excel VBA is the excellent VBA and Macros book. You can check out a copy on Amazon by clicking the image below.

Источник

Excel-VBA Solutions

Want to become an expert in VBA? So this is the right place for you. This blog mainly focus on teaching how to apply Visual Basic for Microsoft Excel. So improve the functionality of your excel workbooks with the aid of this blog. Also ask any questions you have regarding MS Excel and applying VBA. We are happy to assist you.

Pages

Clear Sheet Using VBA

In this post you will learn how to clear a sheet using VBA. Excel sheets can contain various elements and objects. So I will show you how you can clear these various items using VBA. You will learn how to clear a specific range or an entire sheet. Also you will learn how to clear things from an active sheet or a specific sheet. Inbuilt clear methods don’t allow us to delete shapes. So at the end I will show how to create our own VBA code to delete objects such as charts, shapes etc. To explain about these various methods I will use this sample Excel sheet.

This Excel sheet consists of text, values, hyperlink, chart and a star. Also some formattings are applied to the sheet such as colors and borders.

There are various ways to clear Excel sheets. But not all the methods can be used to clear all the things from the sheet. So let’s see how we can use these different methods and their outcomes.

Here is the first method you can use to clear an Excel sheet using VBA.

In this method we haven’t specified a sheet. So the clear method will be applied to the active sheet. If you run the above VBA code, contents, formats, comments and hyperlinks will be deleted from the active sheet. But the shapes and charts will not be deleted. If the source data of the chart is in the same sheet, then the chart will be blanked. This is what happened to our sample Excel sheet after running above code. (In our sample excel sheet, source data of the chart was in the same sheet.)

Next let’s look at how to modify the above VBA code to clear a specific sheet from a workbook. Assume we have a workbook with multiple worksheets and we need to clear the sheet called “Order Details”. You can modify the above subroutine to clear the “Order Details” sheet as follows.

If you want, you can declare a variable of type worksheet and then assign this worksheet to the variable and clear it as well.

Dim WS As Worksheet

Set WS = Worksheets(«Order Details»)

Also the Clear method can be used to clear a specific range of a worksheet. Following subroutine will clear the range A6:B10 of the activesheet.

This is what happened to our sample worksheet after running the above macro.

In the above examples we cleared everything except shapes and charts. However if you want you can clear only particular things from an Excel sheet or a range. Here is the list of things you can clear using VBA, without clearing everything.

Now let’s look at how to clear each of these things separately.

Following subroutine will clear all the comments of the active sheet.

You can use the below macro to clear the contents from range A8:B12.

Only contents are removed from that area keeping the table formats.

Also we can clear only formats from the whole sheet or specific range of a sheet. This next macro will clear all the formats from the table (range A1:B10) of the “Data” sheet.

Here is the result of the above subroutine.

Sometimes you may want to remove the hyperlinks of the entire sheet or from a range of cells using VBA. For that you can use the ClearHyperlinks method as follows.

Above macro will remove all the hyperlinks of the activesheet. However note that the formattings and the text of the hyperlinks will not be removed. Only the link will be removed.

Here is how to clear all the notes of the Excel sheet using VBA.

Next let’s look at how to clear the outline from an Excel sheet. Assume we have an outline like this in our worksheet.

Here we have grouped rows from 2 to 10 using an outline. We can remove this outline automatically using the ClearOutline method as follows. Name of the worksheet is “Data”.

So far we learnt various ways to clear sheets. But none of them were able to delete the objects such as shapes, charts etc. Inbuilt clear methods don’t clear these objects from the sheet. We need to create our own code to delete these items. Next let’s look at how to delete these objects from a worksheet automatically using VBA. To do this we need to iterate through each and every shape using a For Each loop and delete them. Here is the code to delete all the shapes from the active sheet.

Dim Sh As Shape

For Each Sh In ActiveSheet.Shapes
Sh.Delete
Next

If we run this subroutine in our sample Excel sheet, the result will look like this.

Check this post if you want to learn more about manipulating shapes using VBA.

We can also add the Clear method to this subroutine. Then everything will be cleared from the sheet.

Dim Sh As Shape

For Each Sh In ActiveSheet.Shapes
Sh.Delete
Next

Above macro will clear everything in the activesheet. You can also modify the above VBA code to clear everything from a specific sheet as well. Assume we want to clear everything from a sheet called “Data”. For that we can easily modify the above subroutine as follows.

Источник

VBA Delete or Clear Worksheet

In this Article

This tutorial will teach you how to delete or clear a worksheet using VBA.

Delete Worksheet

Use the delete command to delete a worksheet.

Delete Worksheet by Name

Delete Worksheet by Index Number

This code deletes the first worksheet in the workbook:

This code deletes the last worksheet in the workbook:

Delete Worksheet Without Prompt

When you attempt to delete a worksheet, Excel will ask you to confirm your action:

You can disable these prompts (alerts) by toggling DisplayAlerts:

Delete Sheet If It Exists

If you attempt to delete a worksheet that does not exist, VBA will generate an error. With On Error Resume Next you can tell VBA to delete a sheet if it exists, otherwise skip to the next line of code:

You could also use our RangeExists function to check if a sheet exists and if so delete it.:

Clear Sheet

This code will clear an entire sheet of contents, formats, and everything else:

Clear Sheet Contents

This code will clear an entire sheet’s contents. It will leave formatting, comments, and everything else alone:

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

Clear Sheet UsedRange

The above examples will clear ALL cells in a worksheet. This can be very time consuming in large sheets. If you use the UsedRange instead, VBA will only clear the “used” cells that contain values, formats, etc.

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

Источник

Quickest way to clear all sheet contents VBA

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:

It also doesn’t respond. What’s the quickest way to do this?

4 Answers 4

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:

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

EDIT: (@tavnab and @Azura)
Heads up for future readers, you cannot delete a sheet if it’s the last/only one in the workbook.
In that case, you can add the new blank sheet first, delete the old one, and finally rename that new sheet to the old sheet’s name. Also note that eliminating a sheet will create conflicts with formulas in other sheets that were referencing the recently eliminated one, recreating the sheet may not solve that issue.

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:

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

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.)

Источник

Понравилась статья? Поделить с друзьями:
  • Excel shapes select all
  • Excel saved as copy of
  • Excel shape textbox vba excel
  • Excel saveas перезаписать без подтверждения
  • Excel shape in cell