In VBA, the difference between a method's functional invocation and a procedural call

Source: Internet
Author: User

To see someone ask for help in batch setup a hyperlink in a workbook, you try to write a piece of code:

1 Subaddhyperlinks ()2 3 DimStrName as String, source as String, Target as String4 DimI as Integer5 6i =57Source ="Catalogue!A1"8 9  Do  whileCells (I,"D") <>""Ten  OneStrName = Cells (i,"D"). Text Atarget = StrName &"! A1" -      -     'Cells (i, "E") cell links to [A1] cells for worksheets with table name strname theSheets ("Catalogue"). Hyperlinks.add Cells (i,"e"),"", Target -      -     '[A1] cell of a sheet with table name strname, and Reverse link to [A1] cell with table named "Directory" -Sheets (StrName). Hyperlinks.add Sheets (strName). [A1],"", Source, texttodisplay:="Back to Catalog" +      -     'Sheets ("directory"). Hyperlinks.add (Cells (i, "E"), ""). subaddress = Target +     'Sheets (strName). Hyperlinks.add (Sheets (strName). [ A1], "", source). TextToDisplay = "Return to Directory" A      ati = i +1 - Loop -     -    MsgBox "Total Batch setup"& I-5&"Bar Hyperlink" -      - End Sub

The test found that if you use 20/21 or two lines instead of 15/18, the entire code will be much slower. What's the reason? (Description: function = method)

Although it is also "expression." Hyperlinks.add ", but the difference between the two ways is that the parameters are used differently.

Way One (15/18 lines): expression. Hyperlinks.add parameter 1, parameter 2 ...

Way two (20/21 lines): expression. Hyperlinks.add (parameter 1. Parameter 2 ...)

The point is that there are functions in VB that return values:

1, can be called in the form of the procedure, do not parentheses, do not return values; (Way one)
2, can also be called in the form of a function, the parentheses, and there is a return value. (Way II)

If you use the "mode two" Hyperlinks.add return a Hyperlink object (that is, the return value of the method), in the Do...wile loop constantly create objects, obviously greatly affect the efficiency of operation, in this code I just want to hyperlink to facilitate the jump, Do not care about hyperlink objects, so the use of "way one" does not return the value of the call is the best choice.

In addition, this simple function is as below:

1 Function  as Integer  as Integer  as Integer 2     ADD = a + b3End Function

The Add function has a return value, but we can not return a value, so call:

1 Sub Test () 2    1 2 3 End Sub

You can also use the return value to call add:

1 Sub Test () 2     Dim  as Integer 3     result = Add (12)4     MsgBox  result5  End Sub

But if the call as follows, will be an error, the hint "compile error, missing =", the syntax has been wrong.

Sub Test ()     ' Grammatical Errors     Add (A, b)End Sub

This VB syntax error can be understood as a function call has a return value, the return value to be used, it can be used to assign a variable, or as the beginning of the 20th 21 line statement, using the Hyperlinks.add method to create the Hyperlink object (the return value of the method ), assigning values to the Subaddress/texttodisplay of the object, respectively.

But if you call a method with a return value in a function, but do not use the return value, you will get an error.

Personal hobbies, part of the above content is just feeling and speculation.

In VBA, the difference between a method's functional invocation and a procedural call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.