{"id":4248,"date":"2019-04-04T07:45:19","date_gmt":"2019-04-04T07:45:19","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=4248"},"modified":"2024-03-29T15:55:39","modified_gmt":"2024-03-29T15:55:39","slug":"vba-range-rows","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/","title":{"rendered":"VBA range.rows"},"content":{"rendered":"\n

The Rows<\/strong> property\nrepresents all the rows on the specified worksheet or range. <\/p>\n\n\n\n

We are going to use the following example.<\/p>\n\n\n\n

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

Each cell inside the range B2<\/strong> to C7<\/strong> has values.<\/p>\n\n\n\n

Let\u2019s run the following code.<\/p>\n\n\n\n

Delete all rows<\/h2>\n\n\n\n
Sub DeleteAllRows()\n    Range(\"B2:C7\").Rows.Delete\nEnd Sub<\/code><\/pre>\n\n\n\n

It will leave us with a blank worksheet.<\/p>\n\n\n\n

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

That happens because we are deleting all rows<\/a> in the\nselected range<\/a>. In this example, we would have the same effect if we used Range(“B2:C7”).Columns.Delete<\/strong>.<\/p>\n\n\n\n

Delete the selected row<\/h2>\n\n\n\n

In order to delete a single row, we have to use the row number. Let\u2019s delete the second row<\/a> inside the selected range.<\/p>\n\n\n\n

Sub DeleteAllRows()\n    Range(\"B2:C7\").Rows(2).Delete\nEnd Sub<\/code><\/pre>\n\n\n\n

If we run the above code, we are going to get the following\nresult.<\/p>\n\n\n\n

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

The second row is removed from the selected range<\/a>.<\/p>\n\n\n\n

Delete a range of rows<\/h2>\n\n\n\n

You can specify a range inside a range. For example, let\u2019s select the range B2:C7<\/g><\/strong>, and inside this range we are going to select rows 2, 3, and 4 and delete them. We could do this using the following lines of code:<\/p>\n\n\n\n

Range(\"B2:C7\").Rows(2).Delete\nRange(\"B2:C7\").Rows(3).Delete\nRange(\"B2:C7\").Rows(4).Delete<\/code><\/pre>\n\n\n\n

But it\u2019s easier and more elegant to use the following code, where you delete these rows<\/a> using a single line.<\/p>\n\n\n\n

Sub DeleteSelectedRows()\n    Range(\"B2:C7\").Rows(\"2:4\").Delete\nEnd Sub<\/code><\/pre>\n\n\n\n

After you run it, Excel will remove the rows with the given\nnumbers and you are going to get this result.<\/p>\n\n\n\n

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

Count rows<\/h2>\n\n\n\n

If you run the following code, Excel will count all the rows<\/a> inside the entire worksheet.<\/p>\n\n\n\n

Sub CountRowsInsideWorksheet()\n    MsgBox Rows.Count\nEnd Sub<\/code><\/pre>\n\n\n\n

The result is 1,048,576 – the number of rows inside a\nworksheet.<\/p>\n\n\n\n

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

If you want to quickly find the number of rows inside a\nworksheet, press Ctrl + Down Arrow<\/strong>.\nThe cursor will be instantly moved to the last row<\/a>.<\/p>\n\n\n\n

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

In a similar way, you can get the number of rows inside a range.<\/p>\n\n\n\n

Sub CountRowsInsideRange()\n    MsgBox Range(\"B2:C7\").Rows.Count\nEnd Sub<\/code><\/pre>\n\n\n\n

The code returns 6.<\/p>\n\n\n\n

Highlight even rows<\/h2>\n\n\n\n

In a lesson about the Range.Row<\/a> property I showed how you can\nhighlight even rows inside the selected range<\/a>. This time we are going to\nachieve the same result using Range.Rows\nproperty<\/strong>.<\/p>\n\n\n\n

As we know Rows<\/strong> property represents all rows inside the selected range. We have to use indexes to distinguish single rows.<\/p>\n\n\n\n

Sub HighlightEvenRows()\n    Set myRange = Range(\"B2:C7\")\n    For i = 1 To myRange.Rows.Count\n        If i Mod 2 = 0 Then\n            myRange.Rows(i).Interior.ThemeColor = xlThemeColorAccent6\n        End If\n    Next i\nEnd Sub<\/code><\/pre>\n\n\n\n

Code explanation:<\/strong><\/p>\n\n\n\n

2.<\/strong> First, we\nassign the selected range to the myRange<\/strong>\nvariable, so we don\u2019t have to use Range(\u201cB2:C7\u201d)\nall the time.<\/p>\n\n\n\n

3.<\/strong> The loop will\ngo from 1 to myRange.Rows.Count, which is 6.<\/p>\n\n\n\n

4.<\/strong> For each\niteration, it will check whether there is a reminder\nafter division by 2 (modulo). In other\nwords, it will highlight every other row.<\/p>\n\n\n\n

This is the result.<\/p>\n\n\n\n

\"\"<\/figure>\n","protected":false},"excerpt":{"rendered":"

The Rows property represents all the rows on the specified worksheet or range. We are going to use the following example. Each cell…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nVBA range.rows<\/title>\n<meta name=\"description\" content=\"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.\" \/>\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\/vba-range-rows\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA range.rows\" \/>\n<meta property=\"og:description\" content=\"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-04T07:45:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-29T15:55:39+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"VBA range.rows\",\"datePublished\":\"2019-04-04T07:45:19+00:00\",\"dateModified\":\"2024-03-29T15:55:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\"},\"wordCount\":414,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\",\"name\":\"VBA range.rows\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png\",\"datePublished\":\"2019-04-04T07:45:19+00:00\",\"dateModified\":\"2024-03-29T15:55:39+00:00\",\"description\":\"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"VBA range.rows\"}]},{\"@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.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"VBA range.rows","description":"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.","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\/vba-range-rows\/","og_locale":"en_US","og_type":"article","og_title":"VBA range.rows","og_description":"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.","og_url":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/","article_published_time":"2019-04-04T07:45:19+00:00","article_modified_time":"2024-03-29T15:55:39+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png"}],"author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"VBA range.rows","datePublished":"2019-04-04T07:45:19+00:00","dateModified":"2024-03-29T15:55:39+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/"},"wordCount":414,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/","url":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/","name":"VBA range.rows","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png","datePublished":"2019-04-04T07:45:19+00:00","dateModified":"2024-03-29T15:55:39+00:00","description":"on the specified worksheet or range, we can use the code Rows(3).Delete, which will delete the third row.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2019\/04\/rows-example.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/vba-range-rows\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"VBA range.rows"}]},{"@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."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4248"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=4248"}],"version-history":[{"count":9,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4248\/revisions"}],"predecessor-version":[{"id":9806,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4248\/revisions\/9806"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=4248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=4248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=4248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}