Excel vba clearcontents что такое

Home / VBA / VBA ClearContents (from a Cell, Range, or Entire Worksheet)

Written by Puneet for Excel 2007, Excel 2010, Excel 2013, Excel 2016, Excel 2019, Excel for Mac

Key Points

  • To clear content from a cell, you need to use the ClearContents method.
  • You can use it for a single cell, a range, or an entire worksheet.

ClearContents Method

In VBA, there is a method called ClearContents that you can use to clear values and formulas from a cell, range of cells, and the entire worksheet.

Expression.ClearContents

To use this method, first, you need to define the expression somewhere you want to clear the content, and then type “.ClearContents”.

Clear Contents from a Single Cell

Let’s say you want to clear values from cell A1. First, you need to define cell A1 using the range object and then the ClearContents method.

So the code would be:

Sub ClearContentExamples()
Range("A1").ClearContents
End Sub

You can also use the cells property to define a cell and the code would be:

Sub ClearContentExamples()
Cells(1, 1).ClearContents
End Sub

Clear Contents from a Range

In the same way, you can define a range and then use the ClearContent method to clear values and formulas from that range. Let’s say you want to clear values from the range A1:A10, in this case, the code would be something like the below.

Sub ClearContentExamples()
Range("A1:A10").ClearContents
End Sub

Clear Content from an Entire Worksheet

To clear content from an entire worksheet, you can use the below code:

Sub ClearContentExamples()
Cells.ClearContents
End Sub

And to clear from a specific worksheet:

Worksheets("Sheet1").Cells.ClearContents

Clear Content from the Selection

If you want to write a dynamic code that can clear contents from the selected cells, then you can use the below code.

Sub ClearContentExamples()
    Selection.ClearContents
End Sub

Other Methods

Below are some of the other methods that you can use:

Sub ClearContentExamples()
    Range(“A1”).Clear ‘Clears Everything   
    Range(“A1”).ClearComments ‘Clear Comments  
    Range(“A1”).ClearFormats ‘Clears Formatting   
    Range(“A1”).ClearHyperlinks ‘Clear Hyperlinks  
    Range(“A1”).ClearNotes ‘Clear Notes
    Range(“A1”).ClearOutline ‘Clears Outline
End Sub

More Tutorials

    • Count Rows using VBA in Excel
    • Excel VBA Font (Color, Size, Type, and Bold)
    • Excel VBA Hide and Unhide a Column or a Row
    • Excel VBA Range – Working with Range and Cells in VBA
    • Apply Borders on a Cell using VBA in Excel
    • Find Last Row, Column, and Cell using VBA in Excel
    • Insert a Row using VBA in Excel
    • Merge Cells in Excel using a VBA Code
    • Select a Range/Cell using VBA in Excel
    • SELECT ALL the Cells in a Worksheet using a VBA Code
    • ActiveCell in VBA in Excel
    • Special Cells Method in VBA in Excel
    • UsedRange Property in VBA in Excel
    • VBA AutoFit (Rows, Column, or the Entire Worksheet)
    • VBA Copy Range to Another Sheet + Workbook
    • VBA Enter Value in a Cell (Set, Get and Change)
    • VBA Insert Column (Single and Multiple)
    • VBA Named Range | (Static + from Selection + Dynamic)
    • VBA Range Offset
    • VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
    • VBA Wrap Text (Cell, Range, and Entire Worksheet)
    • VBA Check IF a Cell is Empty + Multiple Cells

    ⇠ Back to What is VBA in Excel

    Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes

    Return to VBA Code Examples

    In this Article

    • VBA Clear Cells / Ranges
    • VBA ClearContents
    • VBA Clear
      • VBA Clear Formatting
      • Clear Selection
      • Clear Entire Sheet

    In VBA it’s easy to clear cells or cell properties with the .Clear methods.

    VBA Clear Cells / Ranges

    Type the following into the VBA Editor.

    Range("a1").Clear

    This will display all of the Clear methods available to you:

    vba clear cells

    As you can see, You can clear:

    • Everything ( .Clear)
    • Comments ( .ClearComments)
    • Contents ( .ClearContents)
    • Formats ( .ClearFormats)
    • Hyperlinks ( .ClearHyperlinks)
    • Notes ( .ClearNotes)
    • Outline ( .ClearOutline)

    VBA ClearContents

    The most common clear method is ClearContents. ClearContents clears only the contents of cells (cell values / text). It does not clear formatting, comments, or anything else.

    Range("b2").ClearContents

    vba clearcontents

    ClearContents is the same as pressing the Delete key on your keyboard.

    You can also clear the contents of an entire range of cells:

    Range("b2:c10").ClearContents

    VBA Clear

    Clear will clear all cell properties from a cell:

    Range("b2").Clear

    vba clear

    VBA Clear Formatting

    To clear cell formatting use ClearFormats

    Range("b2").ClearFormats

    vba clear formats

    Clear Selection

    To clear the current selection:

    Selection.Clear

    Clear Entire Sheet

    To clear an entire worksheet:

    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!
    vba save as

    Learn More!

    ClearContents is a method in VBA used to delete or remove the values stored in the cells provided to it. This method makes the cell range empty. It is used with the range property to access the specified cell range. An example of this method is range(“A1:B2”). The ClearContents method will clear the contents of cells from A1 to B2.

    In Excel, adding and deleting data is a common routine task. 

    Sometimes we delete a single cell value, sometimes many cell values, and sometimes we may require to delete the entire worksheet content. This article will show you how to use the “ClearContents” method in Excel VBA. In VBA, we have many methods to do this, like “Clear,” “Delete,” and “Clear Contents.”

    Table of contents
    • Excel VBA Clear Contents
      • What are Clear Contents in Excel VBA?
      • Difference Between Clear & Delete Methods
      • Use VBA Clear Contents Method to Retain Formatting of Cells
      • Loop Through all the Worksheets and Clear Contents of Specific Range
      • Recommended Articles

    VBA Clear Contents

    What are Clear Contents in Excel VBA?

    Before we tell you about ClearContents in VBA, let me show how we can delete or clear off the data in the specific range.

     Look at the below data.

    VBA Clear contents Example 1

    Now, if we want to clear off cells A1 to C3, we need first to mention the range of cells using the VBA RANGERange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more object.

    Code:

    Range (“A1:C3”)

    After mentioning the range of cells by using the RANGE object, we need to select the method “Clear” to clear off the mention of the cell values.

    Code:

    Range (“A1:C3”).Clear

    It will clear off the mentioned cell values.

    Code:

    Sub Clear_Example()
    
      Range("A1:C3").Clear
    
    End Sub

    VBA Clear contents Example 1-1

    VBA Clear contents Example 1-2

    Apart from the clear method, we can also use the “DELETE” method.

    Code:

    Range (“A1:C3”).Delete

    VBA Clear contents Example 1-3

    It will delete the mentioned cell values, just like our clear method.

    VBA Clear contents Example 1-4

    You can use the VBA CELLS propertyCells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel cells.read more with a worksheet name if you want to delete all the cell’s data.

    Worksheets(“Sheet1”).Cells.Delete
    Worksheets(“Sheet1”).Cells.Clear

    Both the above codes will delete the entire worksheet “Sheet1” data. In addition, it will delete the cell values from the first cell to the last cell of the worksheet.

    If you want to delete the present sheet cells, you can use the Active Sheet object.

    ActiveSheet.Cells.Delete or ActiveSheet.Cells.Clear

    Difference Between Clear & Delete Methods

    We know this question should have already played in your mind.

    However, there is a difference between these two methods.

    When you use the method “Delete,” it will delete the cell, and the below cell will take over the position of the deleted cell.

    For example, look at the below image.

    VBA Clear contents Example 1

    Now, we will use the delete method to delete cell A1.

    Code:

    Sub Clear_Example()
    
     Range("A1").Delete
    
    End Sub

    VBA Clear contents Example 2

    We will run this code and see what happens.

    VBA Clear contents Example 2-1

    Look what happened here. As we said, when we deleted cell A1, it got deleted, but cell A2 moved one cell up and occupies the deleted cell. So, it will lead to a data mismatch. So, be careful while using the “Delete” method.

    Now, for the same data, we will clear the method.

    Code:

    Sub Clear_Example()
    
     Range("A1").Clear
    
    End Sub

    VBA Clear contents Example 2-2

    Now, see what happens when we run this code.

    VBA Clear contents Example 2-3

    This code has just vacated cell A1 without altering other cells. Therefore, this looks like a proper method to delete only the part of the cells of the entire data range.

    Use VBA Clear Contents Method to Retain Formatting of Cells

    If you have observed the previous two methods, those two methods not only deleted or cleared off the cells provided. It also deleted the formatting of the cells we have provided.

    VBA Clear contents Example 3

    To retain the formatting of the cells, we need not use neither “Delete” nor “Clear,” but we need to use the VBA “ClearContents” method.

    When you enter the range of cells using a RANGE object, it will show all its properties and methods.

    Example 3-1

    We can access “Delete,” we can access “Clear,” and we can also access “ClearContents” methods.

    Example 3-2

    Select this method.

    Code:

    Sub Clear_Example()
    
      Range("A1:C3").ClearContents
    
    End Sub

    Example 3-3

    It will clear content from A1 to C3 cell, but we will have all the existing formatting.

    Example 3-4

    As you can see in the above picture, we have cell color in VBA, borders, and every formatting associated with those mentioned cells.

    Similarly, we can clear the contents of other sheets as well.

    Worksheets(“Sheet1”).Range(“A1:D10”).ClearContents

    It will clear the contents from cells A1 to D10 in sheet “Sheet1”.

    Similarly, we can delete the other open workbook cells as well.

    Workbooks(“Book1.xlsx”).Worksheets(“Sheet1”).Range(“A1:D10”).ClearContents

    Loop Through all the Worksheets and Clear Contents of Specific Range

    Assume you have many sheets in your workbook. You want to delete the range of cells from A1 to C15. To do this, we must use For Each Loop in VBAVBA For Each Loop helps the user to inspect and analyze the groups of objects or values individually. It even facilitates performing the specific activity for every object or value by passing a statement or group of statements in this reference.read more in all the sheets.

    The below code will do the job.

    Code:

    Sub Clear_All()
    
    Dim Ws As Worksheet
    
    For Each Ws In ActiveWorkbook.Worksheets
    Ws.Range("A1:C15").ClearContents
    Next Ws
    
    End Sub

    Example 4

    Note: You can change the range of cells as per your wish.

    If you want to clear off the entire worksheet data, then you need to use the code below.

    Code:

    Sub Clear_All()
    
     Dim Ws As Worksheet
    
     For Each Ws In ActiveWorkbook.Worksheets
       Ws.Cells.ClearContents
     Next Ws
    
    End Sub

    Example 4-1

    You can download this VBA Clear Contents Excel template here – VBA Clear Contents Template.

    Recommended Articles

    This article has been a guide to VBA Clear Contents. Here, we learn how to use the Clear, Delete, and ClearContents method in VBA to clear data in Excel and some simple to advanced examples. Below are some useful Excel articles related to VBA: –

    • New Line in VBA MsgBox
    • How to Use Timer in VBA?
    • Excel VBA Break For Loop
    • Excel VBA Do Loop
    Skip to content

    VBA ClearContents in Excel — Explained with Examples

    We can use VBA to Clear only the Content of a specific range, cells or entire worksheet. we can use ClearContents method of Range Object. Rang.ClearContents method will clear only the data in the range and the formats like border, font styles, background cell will remain same and will not be disturbed.

    VBA ClearContents Method Excel Range or Cell Object

    Excel VBA to ClearContents a Range – Syntax

    Here is the syntax to clear the contents in a range. You can clear the data in any range using VBA Range.ClearContents method.

    Range(“YourRange”).ClearContents

    Excel VBA to ClearContents in a Range – Examples

    The below macro will show you how to clear the contents in a range using VBA. In this example, we are clearing the data in range “A2 to D10” using VBA.

    Sub VBA_Clear_Contents_Range()
        Range("A2:D10").ClearContents
    End Sub
    

    Excel VBA to Clear Contents in a Range – Instructions

    Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:

    1. Open an Excel Workbook from your start menu or type Excel in your run command
    2. Enter some data in any cells in range “A10 to D10” to test this macro. Also format some cell to test this macro.
    3. Press Alt+F11 to Open VBA Editor or you can go to Developer Table from Excel Ribbon and click on the Visual Basic Command to launch the VBA Editor
    4. Insert a Module from Insert Menu of VBA
    5. Copy the above code (for clearing the contents in a range using VBA) and Paste in the code window(VBA Editor)
    6. Save the file as Macro Enabled Workbook (i.e; .xlsm file format)
    7. Press ‘F5′ to run it or Keep Pressing ‘F8′ to debug the code line by line.

    Now you can observe that the data in the range “A2 to D10” is cleared. You can apply any formats and background color, and try this macro. This will clear only the data and formats will be remained same.

    If you want to clear everything including formats and data, you can use Range.Clear method, to clear the comments in a range you use the Range.ClearComments method.

    Range.ClearContents: Will clear only the content/data of the range, formats will remain same.
    Range.Clear: Will clear the data including cell formats.
    Range.ClearComments: Will clear only the comments, formats and data will remain then same.

    Effortlessly Manage Your Projects and Resources
    120+ Professional Project Management Templates!

    A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

    Save Up to 85% LIMITED TIME OFFER
    Excel VBA Project Management Templates
    All-in-One Pack
    120+ Project Management Templates
    Essential Pack
    50+ Project Management Templates

    Excel Pack
    50+ Excel PM Templates

    PowerPoint Pack
    50+ Excel PM Templates

    MS Word Pack
    25+ Word PM Templates

    Ultimate Project Management Template

    Ultimate Resource Management Template

    Project Portfolio Management Templates

    VBA Reference

    Effortlessly
    Manage Your Projects

    120+ Project Management Templates

    Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

    120+ PM Templates Includes:
    By PNRaoLast Updated: March 2, 2023

    Effectively Manage Your
    Projects and  Resources

    With Our Professional and Premium Project Management Templates!

    ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.

    We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.

    Project Management
    Excel VBA

    Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.

    Analysistabs Logo

    Page load link

    Go to Top

    title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

    Range.ClearContents method (Excel)

    vbaxl10.chm144095

    vbaxl10.chm144095

    excel

    Excel.Range.ClearContents

    8c957fdd-e99d-ca0e-7d2c-4cb1db62639a

    05/10/2019

    high

    Range.ClearContents method (Excel)

    Clears formulas and values from the range.

    Syntax

    expression.ClearContents

    expression A variable that represents a Range object.

    Return value

    Variant

    Example

    This example clears formulas and values from cells A1:G37 on Sheet1, but leaves the cell formatting and conditional formatting intact.

    Worksheets("Sheet1").Range("A1:G37").ClearContents

    [!includeSupport and feedback]

    Понравилась статья? Поделить с друзьями:
  • Excel vba clear all range
  • Excel vba case is or are
  • Excel vba class this
  • Excel vba chr пробел
  • Excel vba calling a sub