{"id":8,"date":"2014-01-23T17:59:58","date_gmt":"2014-01-24T00:59:58","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=8"},"modified":"2016-09-26T09:51:12","modified_gmt":"2016-09-26T16:51:12","slug":"basic-operations","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/basic-operations\/","title":{"rendered":"Basic Operations"},"content":{"rendered":"<h2>1. Procedures<\/h2>\n<p>A procedure is a group of statements that is called and executed as a unit<\/p>\n<p style=\"padding-left: 30px;\">Sub <span style=\"color: #0000ff;\">ProcedureName<\/span> ()<br \/>\nstatements<br \/>\nEnd Sub<\/p>\n<p>We will put all our statements inside procedures. Note: each time a macro is recorded in Excel a sub procedure is automatically generated in a new module.<\/p>\n<h2>2. Variables<\/h2>\n<p>A variable is a reference to a location in memory where data is stored. It&#8217;s good practice to declare the types of your variables. To do this, use the command<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Dim<\/span> variableName As <span style=\"color: #0000ff;\">DataType<\/span><\/p>\n<p>A data type categorizes the values that can be assigned to a variable. Most common data types used are:<\/p>\n<ul>\n<li>Integer<\/li>\n<li>Double<\/li>\n<li>String<\/li>\n<li>Boolean<\/li>\n<li>Variant<\/li>\n<\/ul>\n<p>A constant variable is a variable whose value will never change. To declare a constant, we use<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Const <\/span>variable = value<\/p>\n<p>You can declare an array (a variable that contains a vector of values instead of a single value) in a similar way, either hard-coding the number of elements it contains or not. This command creates a single-dimension array with 5 values: note that counting starts from 0:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Dim<\/span> numbers(4) As <span style=\"color: #0000ff;\">Integer<\/span><\/p>\n<p>We&#8217;ll talk more about arrays on a different page.<\/p>\n<h2>3. Math Operations and Functions<\/h2>\n<h3>Arithmetic Operators<\/h3>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">+, -, *, \/, ^, mod<\/span><\/p>\n<h3>Math functions<\/h3>\n<p style=\"padding-left: 30px;\">For example, Abs(),\u00a0 Sqrt(), Int(), Exp(), Log()&#8230;A more complete list is <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/thc0a116.aspx\">here<\/a>.<\/p>\n<h3>Excel functions in VBA<\/h3>\n<p>You can use Excel functions in VBA with the Application object and WorksheetFunction method:<\/p>\n<p style=\"padding-left: 30px;\">Application.WorksheetFunction<span style=\"color: #0000ff;\">.Sum<\/span>()<br \/>\nApplication.WorksheetFunction<span style=\"color: #0000ff;\">.Average<\/span>()<br \/>\nApplication.WorksheetFunction<span style=\"color: #0000ff;\">.VLookup<\/span>()<br \/>\nApplication.WorksheetFunction<span style=\"color: #0000ff;\">.NormInv<\/span> ()<\/p>\n<p>The arguments for these functions are the same as when we use them in Excel, but now you can use VBA variables:<\/p>\n<p style=\"padding-left: 30px;\">Application.WorksheetFunction.Average(<span style=\"color: #0000ff;\">x, y, z<\/span>)<\/p>\n<p><strong>Note<\/strong>: Use worksheet functions or VBA commands when possible, especially for big sets of data, rather than entering formulas into cells in the spreadsheet. See <a href=\"https:\/\/www.soa.org\/News-and-Publications\/Newsletters\/Compact\/2012\/january\/com-2012-iss42-roper.aspx\">here <\/a>for some more thoughts on making big VBA macros efficient.<\/p>\n<h3>Formulas on the Spreadsheet<\/h3>\n<p>Alternatively, and less efficiently,\u00a0you can enter formulas or functions into cells on the spreadsheet using the Formula method with the Range object.<\/p>\n<p style=\"padding-left: 30px;\">Range(\u201cA1\u201d)<span style=\"color: #0000ff;\">.Formula<\/span> = \u201c=SUM(B1:E1)\u201d<br \/>\nRange(\u201cA1:A5\u201d)<span style=\"color: #0000ff;\">.Formula<\/span> = \u201c=SUM(B1:E1)\u201d<\/p>\n<p>Your formula is a text string that Excel will interpret as a formula, so you can&#8217;t use any commands that are exclusive to VBA. For example, the command in Excel to generate a random number (uniform between 0 and 1) is RAND(), but in VBA it&#8217;s Rnd(). (I know, right?)<\/p>\n<h2>4. Handling Ranges and Cells<\/h2>\n<p><strong>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.<\/p>\n<p style=\"padding-left: 30px;\">Sheets(1).<span style=\"color: #0000ff;\">Range(&#8220;A1&#8221;)<\/span>.Value=3<br \/>\n<span style=\"color: #0000ff;\">Range(&#8220;A1&#8221;)<\/span>.Formula=&#8221;=5*10&#8243;<br \/>\n<span style=\"color: #0000ff;\">Range(&#8220;C1:B3&#8221;)<\/span>.Value=6<\/p>\n<p><strong>Cells method<\/strong>: is similar to the Range method, but takes numeric arguments instead of string arguments:<\/p>\n<p style=\"padding-left: 30px;\">Sheets(1).<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<h2>5. Message Boxes<\/h2>\n<p>Message Boxes allow you to print something for the user to see in a small dialog box; use the MsgBox function:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">MsgBox<\/span> (prompt, [buttons], [title], [helpfile, context])<\/p>\n<p>Input Boxes allow you to prompt the user to enter some value in a small dialog box; use the InputBox function:<\/p>\n<p style=\"padding-left: 30px;\">x = <span style=\"color: #0000ff;\">InputBox<\/span> (prompt, [title], [default], [xpos], [ypos], [helpfile, context])<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Procedures A procedure is a group of statements that is called and executed as a unit Sub ProcedureName () statements End Sub We will put all our statements inside procedures. Note: each time a macro is recorded in Excel a sub procedure is automatically generated in a new module. 2. Variables A variable is [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":2,"menu_order":2,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-8","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/8","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=8"}],"version-history":[{"count":11,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/8\/revisions"}],"predecessor-version":[{"id":1499,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/8\/revisions\/1499"}],"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=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}