{"id":15,"date":"2014-01-24T14:46:13","date_gmt":"2014-01-24T21:46:13","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=15"},"modified":"2016-08-22T13:37:40","modified_gmt":"2016-08-22T20:37:40","slug":"programming-structures","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/programming-structures\/","title":{"rendered":"Programming Structures"},"content":{"rendered":"<h2>1. If, Then Statements<\/h2>\n<p>If-then statements allow you to perform different sets of actions if certain conditions are met or are not met.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">If<\/span> condition1 <span style=\"color: #0000ff;\">Then<\/span><br \/>\nActions1<br \/>\n<span style=\"color: #0000ff;\">ElseIf<\/span> condition2 <span style=\"color: #0000ff;\">Then<\/span><br \/>\nActions2<br \/>\n<span style=\"color: #0000ff;\">Else<\/span><br \/>\nActions3<br \/>\n<span style=\"color: #0000ff;\">End If<\/span><\/p>\n<p>For a single action you can put everything in a single line:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">If<\/span> condition <span style=\"color: #0000ff;\">Then<\/span> action<\/p>\n<h2>2. Select, Case<\/h2>\n<p>The Select-Case statement is used to list possible situations in which certain actions should be performed. This is conceptually the same as an if\/else statement, but easier to read.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #3366ff;\">Select Case<\/span> UserValue<br \/>\n<span style=\"color: #3366ff;\">Case<\/span> 1<br \/>\nActions1<br \/>\n<span style=\"color: #3366ff;\">Case<\/span> 2<br \/>\nActions2<br \/>\n<span style=\"color: #3366ff;\">Case Else<\/span><br \/>\nActionsElse<br \/>\n<span style=\"color: #3366ff;\">End Select<\/span><\/p>\n<p>You can also use the expresions <span style=\"color: #3366ff;\">To, Is =, &lt;&gt;, &lt;, &lt;=, &gt; o &gt;=<\/span><\/p>\n<p style=\"padding-left: 30px;\">Case 1 <span style=\"color: #3366ff;\">To<\/span> 10<br \/>\nCase <span style=\"color: #3366ff;\">Is &lt;=<\/span>10<\/p>\n<h2>3. For, next<\/h2>\n<p>The structure of For, Next loops is as follows:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">For<\/span> i\u00a0= 1 <span style=\"color: #0000ff;\">To<\/span> 10<br \/>\nActions<br \/>\n<span style=\"color: #0000ff;\">Next<\/span> i<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">For<\/span> i = 1 <span style=\"color: #0000ff;\">To<\/span> 10 <span style=\"color: #0000ff;\">Step<\/span> 2<br \/>\nActions<br \/>\n<span style=\"color: #0000ff;\">Next<\/span> i<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">For<\/span> i = 10 <span style=\"color: #0000ff;\">To<\/span> 1 <span style=\"color: #0000ff;\">Step -1<\/span><br \/>\nActions<br \/>\n<span style=\"color: #0000ff;\">Next<\/span> i<\/p>\n<h2>4. Do Loops<\/h2>\n<p>Do Loops perform a set of actions repeatedly while or until a condition is met.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Do While<\/span> count &lt; 10<br \/>\nactions<br \/>\ncount = count + 1<br \/>\n<span style=\"color: #0000ff;\">Loop<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Do Until<\/span> count &gt; 10<br \/>\nactions<br \/>\ncount = count + 1<br \/>\n<span style=\"color: #0000ff;\">Loop<\/span><\/p>\n<p>You can also check the condition after performing the actions<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Do<\/span><br \/>\nactions<br \/>\ncount = count + 1<br \/>\n<span style=\"color: #0000ff;\">Loop While<\/span> count &lt; 10<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Do<\/span><br \/>\nactions<br \/>\ncount = count + 1<br \/>\n<span style=\"color: #0000ff;\">Loop Until<\/span> count &gt; 10<\/p>\n<p><strong>Exiting Loops<\/strong><\/p>\n<p>To exit a loop we use <span style=\"color: #0000ff;\">Exit For<\/span> and <span style=\"color: #0000ff;\">Exit Do<\/span> depending on the loop. The command will stop executing the loop and move to the next line of code after the Next\/Loop statement.<\/p>\n<h2>\u00a05. The With Construct<\/h2>\n<p>The With construct is used to set several properties or use several methods of one object in an enclosed statement.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">With<\/span> Range(\u201cA1:B5\u201d)<br \/>\n.Interior.Color = vbRed<br \/>\n.Font.Bold = True<br \/>\n.Font.Name = \u201cArial\u201d<br \/>\n.Borders(xlEdgeBottom).LineStyle = xlDash<br \/>\n<span style=\"color: #0000ff;\">End With<\/span><\/p>\n<h2>\u00a06. For, each, next<\/h2>\n<p>For Each, Next loops use objects as the loop counter.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">For Each<\/span> ws <span style=\"color: #0000ff;\">In<\/span> ActiveWorkbook.<span style=\"color: #0000ff;\">Worksheets<\/span><br \/>\nactions<br \/>\nNext ws<\/p>\n<p><span style=\"color: #0000ff;\">For Each<\/span> r <span style=\"color: #0000ff;\">in<\/span> Worksheets(&#8220;Sheet1&#8221;).Range(&#8220;A1:D10&#8221;).<span style=\"color: #0000ff;\">Cells<\/span><br \/>\n<span style=\"color: #0000ff;\">For Each<\/span> nm <span style=\"color: #0000ff;\">In<\/span> ActiveWorkbook.<span style=\"color: #0000ff;\">Names<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. If, Then Statements If-then statements allow you to perform different sets of actions if certain conditions are met or are not met. If condition1 Then Actions1 ElseIf condition2 Then Actions2 Else Actions3 End If For a single action you can put everything in a single line: If condition Then action 2. Select, Case The [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":2,"menu_order":3,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-15","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/15","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=15"}],"version-history":[{"count":6,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/15\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/15\/revisions\/1457"}],"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=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}