<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kacper Dziembek</title>
    <description>The latest articles on DEV Community by Kacper Dziembek (@kacper2048).</description>
    <link>https://dev.to/kacper2048</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2875386%2F6ec05ba9-0553-4d30-97ba-f40d97ddf85e.jpg</url>
      <title>DEV Community: Kacper Dziembek</title>
      <link>https://dev.to/kacper2048</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kacper2048"/>
    <language>en</language>
    <item>
      <title>Excel VBA - Subs and Functions</title>
      <dc:creator>Kacper Dziembek</dc:creator>
      <pubDate>Mon, 17 Feb 2025 14:46:58 +0000</pubDate>
      <link>https://dev.to/kacper2048/excel-vba-subs-and-functions-1jh</link>
      <guid>https://dev.to/kacper2048/excel-vba-subs-and-functions-1jh</guid>
      <description>&lt;h2&gt;
  
  
  Sub
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Sub&lt;/strong&gt; procedure is a series of Visual Basic statements enclosed by the &lt;strong&gt;Sub&lt;/strong&gt; and &lt;strong&gt;End Sub&lt;/strong&gt; statements that performs actions but &lt;u&gt;doesn't return a value&lt;/u&gt;. A &lt;strong&gt;Sub&lt;/strong&gt; procedure can take arguments, such as constants, variables, or expressions that are returned by some function. If a Sub procedure has no arguments, the Sub statement must include an empty set of parentheses.&lt;/p&gt;

&lt;p&gt;Pseudo code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sub  &amp;lt;name&amp;gt;( &amp;lt;argument1&amp;gt;, &amp;lt;argument2&amp;gt; , …) 
   &amp;lt;instructions&amp;gt;
End Sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example of real one&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sub GreetUser(name As String, age As Integer) 
   MsgBox("Your answer is correct!", 0, "Answer Box") 
End Sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To call a Sub procedure from another procedure, write the name of the procedure and include values for any required arguments. &lt;strong&gt;The Call statement is not required, but if you use it, you must enclose any arguments in parentheses&lt;/strong&gt;. If you do use &lt;strong&gt;Call&lt;/strong&gt; on a function/Sub, do not include parentheses if the function/Sub does not take any arguments. The following example shows two ways to call a Sub procedure with more than one argument. The second time it is called, parentheses are required around the arguments because the &lt;strong&gt;Call&lt;/strong&gt; statement is used. The following code shows two approaches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sub Main()
    HouseCalc 99800, 43100 
    Call HouseCalc(380950, 49500)
 End Sub 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;To use the return value of a function, assign the function to a variable and enclose the arguments in parentheses&lt;/u&gt;, as shown in the following example. A statement in a Sub or Function procedure can pass values to called procedures by using named arguments. You can list named arguments in any order. A named argument consists of the name of the argument followed by a colon and an equal sign (:=), and the value assigned to the argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answer3 = MsgBox( Title:="Question 3",
Prompt:="Are you happy with your salary?",
Buttons:=4 ) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function
&lt;/h2&gt;

&lt;p&gt;A Function procedure is similar to a Sub procedure, but a function can also return a value and can be used in expressions. A Function procedure can take arguments, such as constants, variables, or expressions that are passed to it by a calling procedure. If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. A function returns a value by assigning a value to its name in one or more statements of the procedure.&lt;/p&gt;

&lt;p&gt;Pseudo Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function &amp;lt;name&amp;gt;(&amp;lt;argument1&amp;gt;, &amp;lt;argument2&amp;gt; , …)
   &amp;lt;instructions&amp;gt;
End Function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function Celsius(fDegrees) 
   Celsius = (fDegrees - 32) * 5 / 9 
End Function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some functions have two versions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variant version: Handles automatic data type conversions and allows Null values but uses more memory.&lt;/li&gt;
&lt;li&gt;String version: More efficient with memory usage but doesn't handle automatic type conversions or Null values.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>excel</category>
      <category>vba</category>
      <category>macro</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
