{"id":26,"date":"2014-01-24T16:04:03","date_gmt":"2014-01-24T23:04:03","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=26"},"modified":"2016-09-26T09:45:07","modified_gmt":"2016-09-26T16:45:07","slug":"ranges","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/ranges\/","title":{"rendered":"Working with Ranges"},"content":{"rendered":"<p>A Range object can represent a single cell, a range of cells, an entire row or column, or a selection containing multiple ranges.<\/p>\n<p>Note that if a worksheet is not specified, VBA will assume that the cells are in the active sheet, which is fine if your code is attached to that specific sheet. If you&#8217;re working with multiple worksheets in the same module, remember to define all of your ranges specifying a worksheet.<\/p>\n<p>Here are some of the more important methods that we use to work with ranges:<\/p>\n<p><strong>1. Setting range variables<\/strong>: use the Set statement to set range variables:<\/p>\n<p style=\"padding-left: 30px;\">Dim myRange as Range<br \/>\n<span style=\"color: #0000ff;\">Set<\/span> myRange = Range(&#8220;A1&#8221;)<\/p>\n<p style=\"padding-left: 30px;\">You can then refer to the range by name, instead of by cell.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">myRange<\/span>.Value=&#8221;hello world&#8221;<\/p>\n<p><strong>2. Range method<\/strong>: returns a range object. The argument is a string that\u2019s either an A1-style reference or the name of a range that was defined in Excel. To name a range in Excel, select the range, right click, and choose Define name.<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/snip.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1497\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/snip-247x300.png\" alt=\"snip\" width=\"247\" height=\"300\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/snip-247x300.png 247w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/snip.png 534w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><\/a><\/p>\n<p style=\"padding-left: 30px;\">Worksheets(&#8220;Sheet1&#8221;).<span style=\"color: #0000ff;\">Range(&#8220;A1&#8221;)<\/span>.Value=3<br \/>\n<span style=\"color: #333399;\">Range(&#8220;A1:A3&#8221;)<\/span>.Value=4<br \/>\nRange(&#8220;range3&#8221;).Formula = &#8220;=5*RAND()&#8221;<\/p>\n<p><strong>3. Cells method<\/strong>: is similar to the Range method, but takes numeric arguments instead of string arguments. Note that the values are (row, column)&#8211;this might feel weird because it&#8217;s sort of like (y,x) instead of (x,y).<\/p>\n<p style=\"padding-left: 30px;\">Worksheets(&#8220;Sheet1&#8221;).<span style=\"color: #0000ff;\">Cells<\/span>(1,1).Value=3<br \/>\n<span style=\"color: #0000ff;\">Cells<\/span>(1,1).Formula=&#8221;=10*RAND()&#8221;<\/p>\n<p>You can combine the range and cells methods to return a region. The following statement selects cells A1:C4<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Range<\/span>(<span style=\"color: #0000ff;\">Cells<\/span>(1,1), <span style=\"color: #0000ff;\">Cells<\/span>(3,4)).select<\/p>\n<p><strong>3. Offset method<\/strong>: takes an input Range object, and RowOffset and ColumnOffset arguments, returning a new range. This is particularly handy in combination with the &#8220;with&#8221; programming structure: you can refer to multiple offset values in a compact way.<\/p>\n<p style=\"padding-left: 30px;\">Range(\u201cA1\u201d).<span style=\"color: #0000ff;\">Offset<\/span>(0,1).Value = 2<br \/>\nWith Range(\u201cA1:B5\u201d)<br \/>\n.<span style=\"color: #0000ff;\">Offset<\/span>(0,1).Value = 2<br \/>\n.<span style=\"color: #0000ff;\">Offset<\/span>(0,2).Value = &#8220;hello&#8221;<br \/>\nEnd With<\/p>\n<p><strong>4. End property<\/strong>: returns the last non-empty cell in a column\/row:<\/p>\n<p style=\"padding-left: 30px;\">ActiveCell.<span style=\"color: #0000ff;\">End<\/span>(<span style=\"color: #800000;\">xlDown<\/span>)<br \/>\nRange(ActiveCell, ActiveCell.<span style=\"color: #0000ff;\">End<\/span>(<span style=\"color: #800000;\">xlDown<\/span>))<\/p>\n<p style=\"padding-left: 30px;\">Other directions: <span style=\"color: #800000;\">xlUp<\/span>, <span style=\"color: #800000;\">xlToLeft<\/span> and <span style=\"color: #800000;\">xlToRight<\/span><\/p>\n<p><strong>5. CurrentRegion property<\/strong>: returns the range of cells bounded by empty rows and empty columns<\/p>\n<p style=\"padding-left: 30px;\">Range(&#8220;A1&#8221;).<span style=\"color: #0000ff;\">CurrentRegion<\/span>.select<\/p>\n<p><strong>6. UsedRange property<\/strong>: returns the rectangular range that contains every nonempty cell of the worksheet.<\/p>\n<p style=\"padding-left: 30px;\">Set myRange=Worksheets(&#8220;Sheet1&#8221;).Range(&#8220;A1&#8221;).UsedRange<\/p>\n<p><strong>7. Union method<\/strong>: create a new range by referring to multiple other ranges.<\/p>\n<p style=\"padding-left: 30px;\">Union(Range(&#8220;A1&#8221;).CurrentRegion, Range(&#8220;A21&#8221;)).select<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Range object can represent a single cell, a range of cells, an entire row or column, or a selection containing multiple ranges. Note that if a worksheet is not specified, VBA will assume that the cells are in the active sheet, which is fine if your code is attached to that specific sheet. If [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":2,"menu_order":4,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-26","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22982"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":8,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/26\/revisions"}],"predecessor-version":[{"id":1498,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/26\/revisions\/1498"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}