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.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


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.

LIMITED TIME OFFER

Leave A Comment


Related pages


how to do pivot tables in excel 2010delete column vbafree estimate template wordhow to use vlookup function in excel 2010sas do until loopvba lookupwhat is a pivot table in excel 2013meeting template excelunlock an excel spreadsheetadd developer tab to ribbon word 2013excel programming tutorialms access vba recordsethow to merge excel columnscopy worksheet vbaconvert vbacombo box in excelexcel vba trimfor loops in excelvba save as xlsxexcel vba copy rangehow to get data from another worksheet in exceldml commands sqlunlock excel cells without passwordexcel based dashboardslookup function in excel 2010shortcut key to show formulas in excelwhat is operand and operatorremove blank cells in excel 2010learning sas by exampleexcel macro print to pdfunlock excel sheet without passwordswot analysis template excelhow to combine tabs in excelvisual basic comboboxvba sheet1default row height in excelhow to select rows in excelms excel row functiongantt chart template powerpoint free downloadvba function excelexcel protected worksheethow do i merge two columns in excelexcel vba msgbox optionsvb6 create text fileexcel macro remove duplicatesconvert excel column width to inchesexcel vba codeduplicate values excelhow to create bar and line chart in excel 2010lock a spreadsheethow to turn on macro in excelhow to put checkbox in excelarray vbatest vba codevba range offsettypes of variables in vbavba scripting.filesystemobjectpivot chart in excel 2013how to combine multiple excel sheets into one sheetmicrosoft excel macro examplesvlook up functionvba read text filecol_index_num vlookuphow to insert a column in excelhow to learn vba for excelms excel schedule templatehow can i unprotect an excel sheet without the passwordunlock a spreadsheetvba opendatabasevba tutorials pdfhighlight duplicates in excel 2003