Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>public function function_name(args as string) as string</code></p> <ul> <li><p><b>public</b> is the access definition. public means accessible across the VBA Project</p></li> <li><p><b>function</b> means that is it a function (<em>meaning it is supposed to return something</em>)</p></li> <li><p><b>function_name</b> is the name of a function (can't start with 1 or underscore)</p></li> <li><p><b>args</b> is the local parameter name to be used within the body of function</p></li> <li><p><b>args as String</b> indicates that the function is expecting the <code>args</code> to be of a <code>String</code> type</p></li> <li><p><b> ...) As String</b> indicates that the function will be returning a <code>String</code> data type. So if you have had dimensioned a String type variable you would be able to assign a value to it using the function. </p></li> </ul> <hr> <p>the standard (default) declaration without explicitly specifying the type to be returned returns a <code>Variant</code> </p> <p>It's the same as declaring a variable without specifying its type.</p> <p><code>Dim aVariable</code> </p> <p>with is equivalent to</p> <p><code>Dim aVariable as Variant</code></p> <p>because <code>Variant</code> is the default type.</p> <p>so the <code>as Variant</code> always exist unless there is a different type specified. And because it's default you do not have to explicitly <em>code</em> it.</p> <p>It's somehow similar to <code>Range("A1").Value</code> and <code>Range("A1")</code> - both are the same because <code>.Value</code> is the default property of a <code>Range</code> object.</p> <p>What happens now is the compiler evaluates what value goes into the <code>aVariable</code> under the hood and assigns that type to the variable.</p> <p>Let's say you have </p> <pre><code>Sub Main() Dim varVariable Dim strVariable As String varVariable = "hello world" strVariable = "hello world" MsgBox "varVariable is of " &amp; TypeName(varVariable) &amp; " type" &amp; vbCrLf &amp; _ "strVariable is of " &amp; TypeName(strVariable) &amp; " type" End Sub </code></pre> <p>Like I've said now both are of <code>String</code> type</p> <p><img src="https://i.stack.imgur.com/wSScQ.png" alt="enter image description here"></p> <hr> <p>Not sure how familiar with for example C# you are but in C# you declare the <strong>return type</strong> of a function right after the access modifier ie.</p> <pre><code>public string myFunction(string args) </code></pre> <p>so in VB/VBA the second <code>as String</code> is equal to the first <code>string</code> (<em>right after public</em>) in C# </p> <p>in C# you would use a <code>return</code> keyword while in VBA you replace the <code>return</code> keyword with the function <strong>name</strong>. Therefore in VBA a very basic sample</p> <pre><code>Public Function ReturnFirst3Characters(args As String) As String ReturnFirst3Characters = IIf(Len(args) &gt; 2, Left(args, 3), args) End Function </code></pre> <p>Function returns first 3 characters of the string you have passed to it (<em>if the string is longer then 3 characters, if not it returns the string you passed to the function</em>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload