{"id":9997,"date":"2022-06-08T14:04:21","date_gmt":"2022-06-08T14:04:21","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=9997"},"modified":"2024-02-19T15:00:22","modified_gmt":"2024-02-19T15:00:22","slug":"clear-range-in-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/","title":{"rendered":"How to Clear Range in Excel VBA"},"content":{"rendered":"\n

With VBA, you can always create a code that will automatically do certain actions, write something in your sheet, create another sheet or chart, populate some cells immediately, and so on. But we can also use it to delete things, just as we used it for creation.<\/p>\n\n\n\n

One of the things we can use VBA for is to clear ranges in Excel. In the example below, we will show how to do it and what are the variations of it.<\/p>\n\n\n\n

Clear Range in Excel VBA<\/h2>\n\n\n\n

The first thing we need to do is to populate a certain range in our sheet. To display various options for clearing the ranges, we will create three different ranges:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

As seen, all three ranges presented in the picture above are basically the same. The only difference is noticeable in range number 3<\/strong>, where we have one comment (marked in Excel with a purple triangle in the upper right corner of the cell).<\/p>\n\n\n\n

For the first thing, we will erase the range number one and will clear all the formats as well: bolded cells, Range 1<\/strong> word is merged and centered and in yellow.<\/p>\n\n\n\n

To do this, we need to go to the VBA editor<\/strong>. In order to do so, we press ALT + F11<\/strong> on our keyboard. After that, on the window that appears, we will right-click on the left window and choose Insert >> Module<\/strong>:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Once clicked, we will be presented with the clear text field, in which we will insert the first code:<\/p>\n\n\n\n

Sub Range1_Cleared()\n    Range(\"A1:C7\").Clear\nEnd Sub<\/code><\/pre>\n\n\n\n

This is what our code looks like in the editor:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

When we execute our code by pressing F5<\/strong> on our keyboard, we will notice that the range A1:C7<\/strong> will be completely erased and that the format will be erased as well: no bold, and cells A1:C3 are not merged and centered<\/strong> as well:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

For the next thing, we will clear the content of range 2<\/strong> but will keep the formats of these cells. To do so, we will write the following code:<\/p>\n\n\n\n

Sub Range2_Cleared()\n    Range(\"E1:G7\").ClearContents\nEnd Sub<\/code><\/pre>\n\n\n\n

This is what our code looks like in the editor:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

When we execute this code, we will notice that only the numbers and text were cleared, but not the formatting itself:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

We can notice that cell E1<\/strong> is still merged and centered and that cells E1:G1<\/strong> are bolded and colored in orange.<\/strong><\/p>\n\n\n\n

So the option ClearContents<\/strong> simply erases only the content itself, but not the formatting.<\/p>\n\n\n\n

The easiest way in which you could find out about all the clearing options in the VBA, is to press CTRL + SPACE<\/strong> right after we define our range and we insert a dot:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

With this, we will be able to see IntelliSense<\/strong>. This is a great way in which VBA can complete our sentences, and we can scroll down to see all the options available. Once we get to the Clear option<\/strong>, we will see other options at our disposal. For this exercise, we will show ClearComments<\/strong> as well, so we will choose that one for range 3<\/strong>:<\/p>\n\n\n\n

Sub Range3_Cleared()\n    Range(\"I1:K7\").ClearComments\nEnd Sub<\/code><\/pre>\n\n\n\n

And in the editor:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

When we execute this code, we will notice that the comments have been cleared from Range 3<\/strong>:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

And we no longer have a purple triangle appearing in the upper right corner, which is exactly what this code was supposed to do.<\/p>\n","protected":false},"excerpt":{"rendered":"

With VBA, you can always create a code that will automatically do certain actions, write something in your sheet, create another sheet or…<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"yoast_head":"\nHow to Clear Range in Excel VBA<\/title>\n<meta name=\"description\" content=\"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Clear Range in Excel VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-08T14:04:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-19T15:00:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png\" \/>\n<meta name=\"author\" content=\"Harun Spahic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harun Spahic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"How to Clear Range in Excel VBA\",\"datePublished\":\"2022-06-08T14:04:21+00:00\",\"dateModified\":\"2024-02-19T15:00:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\"},\"wordCount\":547,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png\",\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\",\"name\":\"How to Clear Range in Excel VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png\",\"datePublished\":\"2022-06-08T14:04:21+00:00\",\"dateModified\":\"2024-02-19T15:00:22+00:00\",\"description\":\"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage\",\"url\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png\",\"contentUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png\",\"width\":733,\"height\":166},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Clear Range in Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\",\"name\":\"Harun Spahic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"caption\":\"Harun Spahic\"},\"sameAs\":[\"http:\/\/human.bsru.ac.th\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Clear Range in Excel VBA","description":"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"How to Clear Range in Excel VBA","og_description":"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.","og_url":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/","article_published_time":"2022-06-08T14:04:21+00:00","article_modified_time":"2024-02-19T15:00:22+00:00","og_image":[{"url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"How to Clear Range in Excel VBA","datePublished":"2022-06-08T14:04:21+00:00","dateModified":"2024-02-19T15:00:22+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/"},"wordCount":547,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png","articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/","name":"How to Clear Range in Excel VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png","datePublished":"2022-06-08T14:04:21+00:00","dateModified":"2024-02-19T15:00:22+00:00","description":"Learn how to clear ranges in Excel using VBA code. Delete specific formats, comments, and more. Code examples included in this tutorial.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#primaryimage","url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png","contentUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/06\/different-ranges.png","width":733,"height":166},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/clear-range-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"How to Clear Range in Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."},{"@type":"Person","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2","name":"Harun Spahic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","caption":"Harun Spahic"},"sameAs":["http:\/\/human.bsru.ac.th\/"]}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9997"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=9997"}],"version-history":[{"count":4,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9997\/revisions"}],"predecessor-version":[{"id":13008,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9997\/revisions\/13008"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=9997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=9997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=9997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}