{"id":5123,"date":"2020-03-26T14:17:12","date_gmt":"2020-03-26T14:17:12","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=5123"},"modified":"2024-03-30T00:03:06","modified_gmt":"2024-03-30T00:03:06","slug":"select-range-using-variables","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/","title":{"rendered":"Selecting a Range Using Variables"},"content":{"rendered":"\n

You can select a range in Excel using the range function:<\/p>\n\n\n\n

Sub selectRange()\n    Range(\"B2:E10\").Select\nEnd Sub<\/code><\/pre>\n\n\n\n

This code selects cells between columns B2<\/strong> and E10<\/strong>.<\/p>\n\n\n\n

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

Selecting rows using variables<\/h2>\n\n\n\n

If you are certain that particular columns are going to be\nused in a worksheet, but you want to control rows, you can type column letters\nand use variables for row numbers.<\/p>\n\n\n\n

This code is going to have the same effect as before, but this time, instead of using numbers, we are going to use variables.<\/p>\n\n\n\n

Sub selectRangeVariables()\n    Dim start_row As Long, last_row As Long\n    start_row = 2\n    last_row = 10\n    Range(\"B\" & start_row & \":E\" & last_row).Select\nEnd Sub<\/code><\/pre>\n\n\n\n

You can easily modify the procedure. Instead of specifying the last row<\/a> position, you can add the number of rows to the start_row.<\/p>\n\n\n\n

Sub selectRangeVariables()\n    Dim start_row As Long, last_row As Long\n    start_row = 2\n    last_row = start_row + 8\n    Range(\"B\" & start_row & \":E\" & last_row).Select\nEnd Sub<\/code><\/pre>\n\n\n\n

The start_row<\/strong> and\nlast_row<\/strong> variables are used inside\nthe Range<\/strong> function, joined with column\nletters. <\/p>\n\n\n\n

Selecting columns using variables<\/h2>\n\n\n\n

There is also a way to specify the column number, instead of\nusing letters. This code is more complicated because columns are not using\nnumbers, therefore a function that converts numbers to column letters is\nnecessary.<\/p>\n\n\n\n

I wrote about such a conversion in one\nof my lessons<\/a>.<\/p>\n\n\n\n

First, create the function.<\/p>\n\n\n\n

Function NumbersToColumns(myCol As Long)\n    If myCol >= 1 And myCol <= 16384 Then\n        iA = Int((myCol - 1) \/ 26)\n        fA = Int(IIf(iA - 1 > 0, (iA - 1) \/ 26, 0))\n        NumbersToColumns = IIf(fA > 0, Chr(fA + 64), \"\") & _\n                        IIf(iA - fA * 26 > 0, _\n                        Chr(iA - fA * 26 + 64), \"\") & _\n                        Chr(myCol - iA * 26 + 64)\n    Else\n        NumbersToColumns = False\n    End If\nEnd Function<\/code><\/pre>\n\n\n\n

Now, you can use it inside the procedure. This time we are going to specify 4 variables, instead of 2.<\/p>\n\n\n\n

Sub selectRangeVariables()\n    Dim start_row As Long, last_row As Long\n    Dim start_column As Long, last_column As Long\n    start_row = 2\n    last_row = 10\n    start_column = 2\n    last_column = 5\n    Range(NumbersToColumns(start_column) & start_row & \":\" & NumbersToColumns(last_column) & last_row).Select\nEnd Sub<\/code><\/pre>\n\n\n\n

Now, you can specify ranges using variables<\/a> for rows and columns.<\/p>\n","protected":false},"excerpt":{"rendered":"

You can select a range in Excel using the range function: This code selects cells between columns B2 and E10. Selecting rows using…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nSelecting a Range Using Variables<\/title>\n<meta name=\"description\" content=\"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.\" \/>\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\/select-range-using-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selecting a Range Using Variables\" \/>\n<meta property=\"og:description\" content=\"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-26T14:17:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-30T00:03:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.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\/select-range-using-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Selecting a Range Using Variables\",\"datePublished\":\"2020-03-26T14:17:12+00:00\",\"dateModified\":\"2024-03-30T00:03:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\"},\"wordCount\":210,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\",\"name\":\"Selecting a Range Using Variables\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png\",\"datePublished\":\"2020-03-26T14:17:12+00:00\",\"dateModified\":\"2024-03-30T00:03:06+00:00\",\"description\":\"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage\",\"url\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png\",\"contentUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png\",\"width\":410,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Selecting a Range Using Variables\"}]},{\"@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":"Selecting a Range Using Variables","description":"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.","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\/select-range-using-variables\/","og_locale":"en_US","og_type":"article","og_title":"Selecting a Range Using Variables","og_description":"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.","og_url":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/","article_published_time":"2020-03-26T14:17:12+00:00","article_modified_time":"2024-03-30T00:03:06+00:00","og_image":[{"url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.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\/select-range-using-variables\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Selecting a Range Using Variables","datePublished":"2020-03-26T14:17:12+00:00","dateModified":"2024-03-30T00:03:06+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/"},"wordCount":210,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/","url":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/","name":"Selecting a Range Using Variables","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png","datePublished":"2020-03-26T14:17:12+00:00","dateModified":"2024-03-30T00:03:06+00:00","description":"Learn how to select a range in Excel using the range function, with code examples for selecting cells between columns B2 and E10 or using variables for row numbers.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#primaryimage","url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png","contentUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2020\/03\/range-selection.png","width":410,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/select-range-using-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Selecting a Range Using Variables"}]},{"@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\/5123"}],"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=5123"}],"version-history":[{"count":5,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/5123\/revisions"}],"predecessor-version":[{"id":12935,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/5123\/revisions\/12935"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=5123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=5123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=5123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}