{"id":3862,"date":"2022-08-20T20:48:34","date_gmt":"2022-08-20T15:18:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/"},"modified":"2022-08-20T20:48:34","modified_gmt":"2022-08-20T15:18:34","slug":"solved-how-to-avoid-using-select-in-excel-vba","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/","title":{"rendered":"(Solved) How to avoid using Select in Excel VBA"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-10717999\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"10717999\" data-parentid=\"10714251\" data-score=\"639\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h3>Some examples of how to avoid select<\/h3>\n<p>Use <code>Dim<\/code>&#8216;d variables<\/p>\n<pre><code>Dim rng as Range\n<\/code><\/pre>\n<p><code>Set<\/code> the variable to the required range. There are many ways to refer to a single-cell range:<\/p>\n<pre><code>Set rng = Range(\"A1\")\nSet rng = Cells(1, 1)\nSet rng = Range(\"NamedRange\")\n<\/code><\/pre>\n<p>Or a multi-cell range:<\/p>\n<pre><code>Set rng = Range(\"A1:B10\")\nSet rng = Range(\"A1\", \"B10\")\nSet rng = Range(Cells(1, 1), Cells(10, 2))\nSet rng = Range(\"AnotherNamedRange\")\nSet rng = Range(\"A1\").Resize(10, 2)\n<\/code><\/pre>\n<p>You <em>can<\/em> use the shortcut to the <code>Evaluate<\/code> method, but this is less efficient and should generally be avoided in production code.<\/p>\n<pre><code>Set rng = [A1]\nSet rng = [A1:B10]\n<\/code><\/pre>\n<p>All the above examples refer to cells on the <em>active sheet<\/em>. Unless you specifically want to work only with the active sheet, it is better to Dim a <code>Worksheet<\/code> variable too:<\/p>\n<pre><code>Dim ws As Worksheet\nSet ws = Worksheets(\"Sheet1\")\nSet rng = ws.Cells(1, 1)\nWith ws\n    Set rng = .Range(.Cells(1, 1), .Cells(2, 10))\nEnd With\n<\/code><\/pre>\n<p>If you <em>do<\/em> want to work with the <code>ActiveSheet<\/code>, for clarity it&#8217;s best to be explicit. But take care, as some <code>Worksheet<\/code> methods change the active sheet.<\/p>\n<pre><code>Set rng = ActiveSheet.Range(\"A1\")\n<\/code><\/pre>\n<p>Again, this refers to the <em>active workbook<\/em>.  Unless you specifically want to work only with the <code>ActiveWorkbook<\/code> or <code>ThisWorkbook<\/code>, it is better to Dim a <code>Workbook<\/code> variable too.<\/p>\n<pre><code>Dim wb As Workbook\nSet wb = Application.Workbooks(\"Book1\")\nSet rng = wb.Worksheets(\"Sheet1\").Range(\"A1\")\n<\/code><\/pre>\n<p>If you <em>do<\/em> want to work with the <code>ActiveWorkbook<\/code>, for clarity it&#8217;s best to be explicit. But take care, as many <code>WorkBook<\/code> methods change the active book.<\/p>\n<pre><code>Set rng = ActiveWorkbook.Worksheets(\"Sheet1\").Range(\"A1\")\n<\/code><\/pre>\n<p>You can also use the <code>ThisWorkbook<\/code> object to refer to the book containing the running code.<\/p>\n<pre><code>Set rng = ThisWorkbook.Worksheets(\"Sheet1\").Range(\"A1\")\n<\/code><\/pre>\n<p>A common (bad) piece of code is to open a book, get some data then close again<\/p>\n<p>This is bad:<\/p>\n<pre><code>Sub foo()\n    Dim v as Variant\n    Workbooks(\"Book1.xlsx\").Sheets(1).Range(\"A1\").Clear\n    Workbooks.Open(\"C:\\Path\\To\\SomeClosedBook.xlsx\")\n    v = ActiveWorkbook.Sheets(1).Range(\"A1\").Value\n    Workbooks(\"SomeAlreadyOpenBook.xlsx\").Activate\n    ActiveWorkbook.Sheets(\"SomeSheet\").Range(\"A1\").Value = v\n    Workbooks(2).Activate\n    ActiveWorkbook.Close()\nEnd Sub\n<\/code><\/pre>\n<p>And it would be better like:<\/p>\n<pre><code>Sub foo()\n    Dim v as Variant\n    Dim wb1 as Workbook\n    Dim  wb2 as Workbook\n    Set wb1 = Workbooks(\"SomeAlreadyOpenBook.xlsx\")\n    Set wb2 = Workbooks.Open(\"C:\\Path\\To\\SomeClosedBook.xlsx\")\n    v = wb2.Sheets(\"SomeSheet\").Range(\"A1\").Value\n    wb1.Sheets(\"SomeOtherSheet\").Range(\"A1\").Value = v\n    wb2.Close()\nEnd Sub\n<\/code><\/pre>\n<p>Pass ranges to your <code>Sub<\/code>s and <code>Function<\/code>s as Range variables:<\/p>\n<pre><code>Sub ClearRange(r as Range)\n    r.ClearContents\n    '....\nEnd Sub\n\nSub MyMacro()\n    Dim rng as Range\n    Set rng = ThisWorkbook.Worksheets(\"SomeSheet\").Range(\"A1:B10\")\n    ClearRange rng\nEnd Sub\n<\/code><\/pre>\n<p>You should also apply Methods (such as <code>Find<\/code> and <code>Copy<\/code>) to variables:<\/p>\n<pre><code>Dim rng1 As Range\nDim rng2 As Range\nSet rng1 = ThisWorkbook.Worksheets(\"SomeSheet\").Range(\"A1:A10\")\nSet rng2 = ThisWorkbook.Worksheets(\"SomeSheet\").Range(\"B1:B10\")\nrng1.Copy rng2\n<\/code><\/pre>\n<p>If you are looping over a range of cells it is often better (faster) to copy the range values to a variant array first and loop over that:<\/p>\n<pre><code>Dim dat As Variant\nDim rng As Range\nDim i As Long\n\nSet rng = ThisWorkbook.Worksheets(\"SomeSheet\").Range(\"A1:A10000\")\ndat = rng.Value  ' dat is now array (1 to 10000, 1 to 1)\nfor i = LBound(dat, 1) to UBound(dat, 1)\n    dat(i,1) = dat(i, 1) * 10 ' Or whatever operation you need to perform\nnext\nrng.Value = dat ' put new values back on sheet\n<\/code><\/pre>\n<p>This is a small taster for what&#8217;s possible.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">12<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to avoid using Select in Excel VBA <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Some examples of how to avoid select Use Dim&#8216;d variables Dim rng as Range Set the variable to the required range. There are many ways to refer to a single-cell range: Set rng = Range(&#8220;A1&#8221;) Set rng = Cells(1, 1) Set rng = Range(&#8220;NamedRange&#8221;) Or a multi-cell range: Set rng = Range(&#8220;A1:B10&#8221;) Set rng &#8230; <a title=\"(Solved) How to avoid using Select in Excel VBA\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\" aria-label=\"More on (Solved) How to avoid using Select in Excel VBA\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[400,401],"class_list":["post-3862","post","type-post","status-publish","format-standard","hentry","category-solved","tag-excel","tag-vba"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) How to avoid using Select in Excel VBA - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) How to avoid using Select in Excel VBA - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Some examples of how to avoid select Use Dim&#8216;d variables Dim rng as Range Set the variable to the required range. There are many ways to refer to a single-cell range: Set rng = Range(&quot;A1&quot;) Set rng = Cells(1, 1) Set rng = Range(&quot;NamedRange&quot;) Or a multi-cell range: Set rng = Range(&quot;A1:B10&quot;) Set rng ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T15:18:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\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:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) How to avoid using Select in Excel VBA\",\"datePublished\":\"2022-08-20T15:18:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\"},\"wordCount\":276,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"excel\",\"vba\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\",\"name\":\"(Solved) How to avoid using Select in Excel VBA - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T15:18:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) How to avoid using Select in Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) How to avoid using Select in Excel VBA - JassWeb","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:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) How to avoid using Select in Excel VBA - JassWeb","og_description":"[ad_1] Some examples of how to avoid select Use Dim&#8216;d variables Dim rng as Range Set the variable to the required range. There are many ways to refer to a single-cell range: Set rng = Range(\"A1\") Set rng = Cells(1, 1) Set rng = Range(\"NamedRange\") Or a multi-cell range: Set rng = Range(\"A1:B10\") Set rng ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T15:18:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) How to avoid using Select in Excel VBA","datePublished":"2022-08-20T15:18:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/"},"wordCount":276,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["excel","vba"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/","name":"(Solved) How to avoid using Select in Excel VBA - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T15:18:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-avoid-using-select-in-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) How to avoid using Select in Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3862"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3862\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}