Clear sheets in excel vba

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

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

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.

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:


Sheets("INSERT WORKSHEET NAME HERE").Cells.ClearContents

Example

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

excel vba clear entire sheet

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

Sub Clearsheet()

    Sheets("Sheet1").Cells.ClearContents

End Sub

In Excel this should look something like this:

excel vba clear entire sheet

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

excel vba clear entire sheet

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:


Sub Clearsheet()

    Sheets("Sheet1").Cells.Delete

End Sub

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:



Sub Clearsheet()

    Sheets("Sheet1").Delete

End Sub

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.

Keep Excelling,

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.

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.

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.

Sub ClearSheet_Example1()

     Cells.Clear

End Sub

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

Result of Cells.Clear method

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.

Sub ClearSheet_Example2a()

     Worksheets(«Order Details»).Cells.Clear

End Sub

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

Sub ClearSheet_Example2b()

     Dim WS As Worksheet

     Set WS = Worksheets(«Order Details»)

     WS.Cells.Clear

End Sub

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.

Sub ClearSheet_Example3()

     Range(«A6:B10»).Clear

End Sub

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

Clear a range of an excel sheet using VBA

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.

  • Comments
  • Contents
  • Formats
  • Hyperlinks
  • Notes
  • Outlines

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

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

Sub ClearComments()

     Cells.ClearComments

End Sub

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

Sub ClearContents()

     Range(«A8:B12»).ClearContents

End Sub

Only contents are removed keeping the formats

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.

Sub ClearFormats()

     Worksheets(«Data»).Range(«A1:B10»).ClearFormats

End Sub

Here is the result of the above subroutine.

Only formats are cleared from the specified range

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.

Sub ClearHyperlinks()

     Cells.ClearHyperlinks

End Sub

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.

Sub ClearNotes()

     Cells.ClearNotes

End Sub

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

Outline

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

Sub ClearOutline()

     Worksheets(«Data»).Cells.ClearOutline

End Sub

Outline was cleared using ClearOutline method

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.

Sub DeleteAllObjects()

     Dim Sh As Shape

     For Each Sh In ActiveSheet.Shapes
         Sh.Delete
     Next

End Sub

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

All the shapes are deleted from the sheet

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

Shapes

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

Sub ClearEverything()

     Dim Sh As Shape

     For Each Sh In ActiveSheet.Shapes
         Sh.Delete
     Next

     Cells.Clear

End Sub

Everything was cleared from the worksheet

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.

Sub ClearEverything_SpecificSheet()

     Dim WS As Variant
     Dim Sh As Shape

     Set WS = Worksheets(«Data»)

     For Each Sh In WS.Shapes
         Sh.Delete
     Next

     WS.Cells.Clear

End Sub

In this post, let’s have a look at different options that you have to Delete and Clear worksheet in Excel VBA.

Table of Contents

  • Delete Worksheet
    • Delete Worksheet by Name
    • Delete Worksheet by Index Number
    • Delete Worksheet Without Prompt
  • Clear Sheet in Excel VBA
    • Clear ActiveSheet
    • Clear Contents
    • Clear Formats
    • Clear Sheet (By Name)

To run the VBA, first you will need to do the following.

  • Under the Developer tab, click the Visual Basic option.
  • On the insert option in the menu and click the module
  • Enter the code and run it.

To delete a worksheet in excel VBA, you can use the delete command. There are different ways in which can delete the worksheet in Excel.

Delete Worksheet by Name

To Delete Worksheet by Name

Code

Delete Worksheet by Index Number

To Delete Worksheet by Index Number

Code

Here, 1 represents the index number of the sheets. To delete the last worksheet in the workbook

Code

Sheets (Sheets.Count).Delete

Delete Worksheet Without Prompt

You can disable the prompts while deleting the worksheets. You can do this by toggling DisplayAlerts:

How to Delete and Clear Worksheet in Excel VBA ?
Application.DisplayAlerts = False

Sheets("Sheet1").Delete

Application.DisplayAlerts = True
How to Delete and Clear Worksheet in Excel VBA ?

You can see that the worksheet gets deleted without a prompt.

Clear Sheet in Excel VBA

To clear sheet in Excel VBA, you can use the clear command

Clear ActiveSheet

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

Code

Clear Contents

To clear only the cell contents

Code:

Clear Formats

To clear only the cell formats

Code:

Clear Sheet (By Name)

To clear a specific worksheet

Code

Sheets("Sheet1").Cells.Clear

Понравилась статья? Поделить с друзьями:
  • Circle the word that is different straight long blonde beard
  • Clear excel cells with vba
  • Circle the word that is different socks gloves trainers sandals
  • Clear columns in word
  • Circle the word that is different prawns mussels duck squid