{"id":531,"date":"2013-03-03T14:25:02","date_gmt":"2013-03-03T14:25:02","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=531"},"modified":"2020-04-23T10:57:53","modified_gmt":"2020-04-23T10:57:53","slug":"excel-vba-lesson-22-checkbox","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-22-checkbox\/","title":{"rendered":"Excel VBA Lesson 22: Working with Checkbox and Option Button"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/\">&lt;&lt;Lesson 21&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-23-listbox-combobox\/\">&lt;&lt;Lesson 23&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>There are many Excel VBA controls that can be used&nbsp;to perform certain tasks by writing Excel VBA code for them. These controls are also known as Active-X controls. These controls are Excel VBA objects so they have their own properties, methods and events. They can be found on the Excel Control Toolbox, as shown in theFigure 17.1. We shall deal with the checkbox, the textbox and the option button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"182\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\" alt=\"vba_Figure18.1\" class=\"wp-image-535\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 22.1<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>22.1 The CheckBox<\/strong><\/h3>\n\n\n\n<p>The Checkbox is a very useful control in Excel VBA. It allows the user to select one or more items by checking the check box or check boxes concerned. For example, you may create a shopping cart where the user can click on check boxes that correspond to the items they intend to buy, and the total payment can be computed at the same time.One of the most important properties of the checkbox is Value. If the check box is selected or checked, the value is true, whilst if it is not selected or unchecked, the Value is False.<br>\nThe usage of the checkbox is illustrated in Example 22.1<\/p>\n\n\n\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\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 22.1<\/strong><\/h4>\n\n\n\n<p>In this example, the user can choose to display the sale volume of one type of fruits sold or total sale volume. The code is shown below:<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<\/p>\n\n\n\n<p>If CheckBox1.Value = True And CheckBox2.Value = False Then<br>\nMsgBox &#8220;Quantity of apple sold is\u201d &amp; Cells (2, 2).Value<br>\nElseIf CheckBox2.Value = True And CheckBox1.Value = False Then<br>\nMsgBox &#8220;Quantity of orange sold is &#8221; &amp; Cells(2, 3).Value<br>\nElse<br>\nMsgBox &#8220;Quantity of Fruits sold is\u201d &amp; Cells (2, 4).Value<br>\nEnd If<\/p>\n\n\n\n<p>End Sub<\/p>\n\n\n\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><br>\nThe Interface is shown in Figure 22.2<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"335\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.2.jpg\" alt=\"vba_Figure18.2\" class=\"wp-image-538\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 22.2<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>22.2 The&nbsp;TextBox<\/strong><\/h3>\n\n\n\n<p>The TextBox is the standard Excel VBA control for accepting input from the user as well as to display the output. It can handle string (text) and numeric data but not images.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 22.2<\/strong><\/h4>\n\n\n\n<p>In this example, we inserted two text boxes and display the sum of numbers entered into the two text boxes in a message box. The Val function is used to convert the string into numeric values because the text box treats the number entered as a string.<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click ()<br>\nDim x As Variant, y As Variant, z As Variant<br>\nx = TextBox1.Text<br>\ny = TextBox2.Text<br>\nz = Val(x) + Val(y)<br>\nMsgBox &#8220;The Sum of &#8221; &amp; x &amp; &#8221; and &#8221; &amp; y &amp; &#8221; is &#8221; &amp; z<br>\nEnd Sub<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.3.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"393\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.3.jpg\" alt=\"vba_Figure18.3\" class=\"wp-image-540\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 17.3<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>22.3 The Option Button<\/strong><\/h3>\n\n\n\n<p>The option button control also lets the user selects one of the choices. However, two or more option buttons must work together because as one of the option buttons is selected, the other option button will be deselected. In fact, only one option button can be selected at one time. When an option button is selected, its value is set to \u201cTrue\u201d and when it is deselected; its value is set to \u201cFalse\u201d.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 22.3<\/strong><\/h4>\n\n\n\n<p>This example demonstrates the usage of the option buttons. In this example, the Message box will display the option button selected by the user. The output interface is shown in Figure 22.4.<\/p>\n\n\n\n<p><strong>The code<\/strong><\/p>\n\n\n\n<p>Private Sub OptionButton1_Click ()<\/p>\n\n\n\n<p>MsgBox &#8220;Option 1 is selected&#8221;<br>\nEnd Sub<br>\nPrivate Sub OptionButton2_Click()<br>\nMsgBox &#8220;Option 2 is selected&#8221;<br>\nEnd Sub<br>\nPrivate Sub OptionButton3_Click()<br>\nMsgBox &#8220;Option 3 is selected&#8221;<\/p>\n\n\n\n<p>End Sub<\/p>\n\n\n\n<p>The Output Interface<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.4.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"334\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.4.jpg\" alt=\"vba_Figure18.4\" class=\"wp-image-542\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 22.4<\/strong><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><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21-the-workbook-object\/\">&lt;&lt;Lesson 21&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-23-listbox-combobox\/\">&lt;&lt;Lesson 23&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 21&gt;&gt; [Contents] &lt;&lt;Lesson 23&gt;&gt; There are many Excel VBA controls that can be used&nbsp;to perform certain tasks by writing Excel VBA code for them. These controls are also known as Active-X controls. These controls are Excel VBA objects so they have their own properties, methods and events. They can be found on the Excel &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-22-checkbox\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 22: Working with Checkbox and Option Button&#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":[15],"tags":[],"class_list":["post-531","page","type-page","status-publish","hentry","category-buttons-and-boxes"],"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 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson shows users how to work with controls 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_lesson22.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 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson shows users how to work with controls in Excel VBA macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson22.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:57:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"638\" \/>\n\t<meta property=\"og:image:height\" content=\"182\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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-22-checkbox\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm\",\"name\":\"Excel VBA Lesson 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\",\"datePublished\":\"2013-03-03T14:25:02+00:00\",\"dateModified\":\"2020-04-23T10:57:53+00:00\",\"description\":\"This Excel VBA lesson shows users how to work with controls in Excel VBA macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson22.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg\",\"width\":638,\"height\":182},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson22.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 22: Working with Checkbox and Option Button\"}]},{\"@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 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson shows users how to work with controls 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_lesson22.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson shows users how to work with controls in Excel VBA macro programming","og_url":"https:\/\/excelvbatutor.com\/vba_lesson22.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:57:53+00:00","og_image":[{"width":638,"height":182,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-22-checkbox\/","url":"https:\/\/excelvbatutor.com\/vba_lesson22.htm","name":"Excel VBA Lesson 22: Working with Checkbox and Option Button - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg","datePublished":"2013-03-03T14:25:02+00:00","dateModified":"2020-04-23T10:57:53+00:00","description":"This Excel VBA lesson shows users how to work with controls in Excel VBA macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson22.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson22.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson22.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_Figure18.1.jpg","width":638,"height":182},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson22.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 22: Working with Checkbox and Option Button"}]},{"@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\/531","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=531"}],"version-history":[{"count":31,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/531\/revisions"}],"predecessor-version":[{"id":3466,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/531\/revisions\/3466"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}