Excel VBA Custom Function Authoring (UDF, user-defined function)

Source: Internet
Author: User

While it is known that Microsoft Office Excel can support the writing of complex programming and custom functions in the VB language, there has been no such requirement.

The problem is to calculate a value based on a list of arrays, but the computational process is more complex, it takes a few steps, if you do not program, you have to go through some intermediate cells to complete the calculation, but this will make the whole table is very bloated, and it is inconvenient to add new columns.

In this case, writing a custom function is almost the only way out. Fortunately, it is not too much trouble.

1. First, you may want to enable the development tool in the options, save the file type will be replaced by XLSM.

2. Open the VB Editor to write code, add modules, write their own functions, write finished do not need to compile, in the Excel table can be used.

3. This VB compiler or gray often the primary, so write code is not very convenient is inevitable, hint and complement the function are weaker, endure. In addition, not familiar with the class structure of Excel related to writing code is also difficult to write, fortunately I just wrote some simple operation.

4. Even for simple operation of cells, because there are many situations, such as not valid values, it is very troublesome to write themselves, and the most convenient way is to call the system the original method.

Post a function of this write, is a list of proceeds to seek the maximum drawdown.

1 FunctionJdrawback (rrange asRange)2     DimN as Long3     DimD as Double4     DimCurrentmax as Double5     DimCurrentmaxdrawback as Double6Currentmaxdrawback =07 8      for  eachClinchrrange9D =Worksheetfunction.sum (cl, D)Ten         IfD > Currentmax Then OneCurrentmax =D A         End If -  -         Ifcurrentmax-d > Currentmaxdrawback Then theCurrentmaxdrawback = Currentmax-D -         End If -     NextCL -  +Jdrawback =Currentmaxdrawback - End Function            

Excel VBA Custom Function Authoring (UDF, user-defined function)

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.