{"id":462,"date":"2013-03-03T06:01:10","date_gmt":"2013-03-03T06:01:10","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=462"},"modified":"2020-04-23T10:56:07","modified_gmt":"2020-04-23T10:56:07","slug":"excel-vba-lesson-18-excel-vba-objects","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-18-excel-vba-objects\/","title":{"rendered":"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-17-part-1-excel-vba-object\/\">&lt;&lt;Lesson 17&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-19-range-object\/\">&lt;&lt;Lesson 19&gt;&gt;<\/a><\/strong><\/h4>\n<p>In lesson 13, we have learned the concepts of Excel VBA objects. You have also learned that an Excel VBA object has properties and methods. We have already dealt with properties and we shall learn about methods in this lesson.<\/p>\n<h3><strong>18.1: Methods<\/strong><\/h3>\n<p>A method of an Excel VBA object normally do something or perform certain operations. For example, ClearContents is a method of the range object that clears the contents of a cell or a range of cells. For example, You can write the following code to clear the contents of certain range:<\/p>\n<h4><strong>Example 18.1<\/strong><\/h4>\n<p>&#8216;Clear contents from cells A1 to A6<br \/>\nPrivate Sub CommandButton1_Click()<br \/>\nRange(\u201cA1:A6\u201d).ClearContents<br \/>\nEnd Sub<br \/>\nYou can also let the user select his own range of cells and clear the contents by using the InputBox function, as shown in Example 18.2<br \/>\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br \/>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<h4><strong>Example 18.2<\/strong><\/h4>\n<p>Private Sub CommandButton1_Click()<br \/>\nDim, selectedRng As String<br \/>\nselectedRng = InputBox(&#8220;Enter your range&#8221;)<br \/>\nRange(selectedRng).ClearContents<br \/>\nEnd Sub<\/p>\n<p>In order to clear the contents of the entire worksheet, you can use the following code:<\/p>\n<p><strong>Sheet1.Cells.ClearContents<\/strong><\/p>\n<p>But if you only want to clear the formats of an entire worksheet, you can use the following syntax:<\/p>\n<p><strong>Sheet1.Cells.ClearFormats<\/strong><\/p>\n<p>To select a range of cells, you can use the <strong>Select<\/strong> method. This method selects a range of cells specified by the Range object. The syntax is<br \/>\nRange(\u201cA1:A5\u201d).Select<\/p>\n<h4><strong>Example 18.3<\/strong><\/h4>\n<p>Private Sub CommandButton1_Click()<br \/>\nRange(&#8220;A1:A5&#8221;).Select<br \/>\nEnd Sub<\/p>\n<h4><strong>Example 18.4<\/strong><\/h4>\n<p>This example allows the user to specifies the range of cells to be selected.<\/p>\n<p>Private Sub CommandButton1_Click()<br \/>\nDim selectedRng As String<br \/>\nselectedRng = InputBox(&#8220;Enter your range&#8221;)<br \/>\nRange(selectedRng).Select<br \/>\nEnd Sub<br \/>\nTo deselect the selected range, we can use the Clear method.<br \/>\nRange(\u201cCiRj:CmRn\u201d).Clear<\/p>\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br \/>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<h4><strong>Example 18.5<\/strong><\/h4>\n<p>In this example, we insert two command buttons, the first one is to select the range and the second one is to deselect the selected range.<br \/>\nPrivate Sub CommandButton1_Click()<br \/>\nRange(&#8220;A1:A5&#8221;).Select<br \/>\nEnd Sub<\/p>\n<p>Private Sub CommandButton2_Click()<br \/>\nRange(&#8220;A1:A5&#8221;).Clear<br \/>\nEnd Sub<\/p>\n<p>Instead of using the Clear method, you can also use the ClearContents method.<\/p>\n<p>Another very useful method is the Autofill method. This method performs an autofill on the cells in the specified range with a series of items including numbers, days of the week, months of year and more. The syntax is<\/p>\n<p>Expression.AutoFill(Destination, Type)<\/p>\n<p>Where Expression can be an object or a variable that returns an object. Destination means the required Range object of the cells to be filled. The destination must include the source range. Type means a type of series, such as days of the week, a month of the year and more. The AutoFill type constant is something like xlFillWeekdays, XlFillDays, XlFillMonths and more.<\/p>\n<h4><strong>Example 18.6<\/strong><\/h4>\n<p>Private Sub CommandButton1_Click()<br \/>\nRange(\u201cA1\u201d)=1<br \/>\nRange(\u201cA2\u201d)=2<br \/>\nRange(&#8220;A1:A2&#8221;).AutoFill Destination:=Range(&#8220;A1:A10&#8221;)<br \/>\nEnd Sub<\/p>\n<p>In this example, the source range is A1 to A2. When the user clicks on the command button, the program will first fill cell A1 with 1 and cell A2 will 2, and then automatically fills the Range A1 to A10 with a series of numbers from 1 to 10.<\/p>\n<h4><strong>Example 18.7<\/strong><\/h4>\n<p>Private Sub CommandButton1_Click()<br \/>\nCells(1, 1).Value = &#8220;Monday&#8221;<br \/>\nCells(2, 1).Value = &#8220;Tuesday&#8221;<br \/>\nRange(&#8220;A1:A2&#8221;).AutoFill Destination:=Range(&#8220;A1:A10&#8221;), Type:=xlFillDays<br \/>\nEnd Sub<\/p>\n<h4><strong>Example 18.8<\/strong><\/h4>\n<p>This example allows the user to select the range of cells to be automatically filled using the Autofill method. This can be achieved with the use of the InputBox. Since each time we want to autofill a new range, we need to clear the contents of the entire worksheet using the Sheet1.Cells.ClearContents statement.<\/p>\n<p>Private Sub CommandButton1_Click()<br \/>\nDim selectedRng As String<br \/>\nSheet1.Cells.ClearContents<br \/>\nselectedRng = InputBox(&#8220;Enter your range&#8221;)<br \/>\nRange(&#8220;A1&#8221;) = 1<br \/>\nRange(&#8220;A2&#8221;) = 2<br \/>\nRange(&#8220;A1:A2&#8221;).AutoFill Destination:=Range(selectedRng)<br \/>\nEnd Sub<\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-17-part-1-excel-vba-object\/\">&lt;&lt;Lesson 17&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">&nbsp;[Contents]&nbsp;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-19-range-object\/\">&lt;&lt;Lesson 19&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 17&gt;&gt; [Contents] &lt;&lt;Lesson 19&gt;&gt; In lesson 13, we have learned the concepts of Excel VBA objects. You have also learned that an Excel VBA object has properties and methods. We have already dealt with properties and we shall learn about methods in this lesson. 18.1: Methods A method of an Excel VBA object normally &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-18-excel-vba-objects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-462","page","type-page","status-publish","hentry","category-object"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson18.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson18.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T10:56:07+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-18-excel-vba-objects\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson18.htm\",\"name\":\"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"datePublished\":\"2013-03-03T06:01:10+00:00\",\"dateModified\":\"2020-04-23T10:56:07+00:00\",\"description\":\"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson18.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson18.htm\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson18.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming","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:\/\/excelvbatutor.com\/vba_lesson18.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming","og_url":"https:\/\/excelvbatutor.com\/vba_lesson18.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:56:07+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-18-excel-vba-objects\/","url":"https:\/\/excelvbatutor.com\/vba_lesson18.htm","name":"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2 - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"datePublished":"2013-03-03T06:01:10+00:00","dateModified":"2020-04-23T10:56:07+00:00","description":"This Excel VBA lesson explains the concept of methods of an object and teach you how to use them in Excel VBA macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson18.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson18.htm"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson18.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 18: Introduction to Excel VBA Objects Part 2"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=462"}],"version-history":[{"count":31,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/462\/revisions"}],"predecessor-version":[{"id":3462,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/462\/revisions\/3462"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}