+ Reply to Thread
Results 1 to 10 of 10

Learning VBA: Need Some Explanations on Language

  1. #1
    Registered User
    Join Date
    04-08-2010
    Location
    Montreal
    MS-Off Ver
    2003 & 2010
    Posts
    16

    Learning VBA: Need Some Explanations on Language

    Hi.
    I'm new to this forum so let me introduce myself. I am a final year undergraduate in Finance and I'm teaching myself VBA programming to be ready to enter the job market.

    I have a copy of "Option Pricing Models & Volatility Using Excel-VBA" and I'm going through the examples in the first chapter.

    Let me show you the code:
    Please Login or Register  to view this content.
    I should probably show you one of the previous functions in the program:
    Please Login or Register  to view this content.
    Now, I understand most of the code, but I still don't have a clue on the following:
    1. Is cNum a variable type?
    2. What does setcnum() mean?
    3. What do .rP and .iP mean? What is the syntax?
    4. What does the line "complexop2 = output" exactly do?

    I hope that somebody can help me out. Thanks in advance!
    Last edited by royUK; 04-08-2010 at 02:18 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Learning VBA: Need Some Explanations on Language

    Hello Agimcomas,

    Welcome to the Forum!

    There are some rules you need to be aware of when you post.
    1. Your thread title should clearly describe your problem or question.
    2. To make the code easier to read and copy, format it with Code Tags.

    Because you are new, I have wrapped the code for you.

    As a new member, please take some time to familiarize yourself with the rules.
    Forum Rules
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Learning VBA: Need Some Explanations on Language

    I can only answer this partially and I may have missed bits.

    Is cNum a variable type? it is a class type. Dim cNum1 As cNum defines a variable cNum1 to be of type cNum.

    What does setcnum() mean? Setcnum is the function that you have defined in your second piece of code

    What do .rP and .iP mean? What is the syntax? these are properties of another class

    What does the line "complexop2 = output" exactly do? Output was defined (Dim statement) as a 2 element array containg doubles. This makes the function complexop2 return this doublex2 array


    click on the * Add Reputation if this was useful or entertaining.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,725

    Re: Learning VBA: Need Some Explanations on Language

    Since you are new at this let me try to give a somewhat more detailed explanation, although without the book you are looking at it's hard to give gobs of detail.

    1. Is cNum a variable type? It is either a user-defined variable, or a class. A user-defined variable can have subfields. Here's an example from Excel Help:
    Please Login or Register  to view this content.
    You declare variables using it as a type, just like any other type. More on this in #3.

    2. What does setcnum() mean? I am assuming you made a typing error and this is actually Set_cNum. It is a function call. When you call a function, the call is replaced at runtime by the value returned by the function. See more at #3.

    3. What do .rP and .iP mean? What is the syntax? rP and iP are subfields of the data type (or possibly members of the class) cNum. If you declare a variable

    Please Login or Register  to view this content.
    then you can refer to the subfields of that variable with the dot notation as in this example:
    Please Login or Register  to view this content.
    In the code where you are seeing this, they are used after the function name, which bears additional explanation. When you see
    Please Login or Register  to view this content.
    it means this function is going to return an item that is type cNum. A function always returns a variable that is the same as the function name. So this line of code means that the function Set_cNum is going to return a variable of type cNum that has its rP subfield set to rPart, which also happens to be an argument for the function. So this function has the purpose of taking two values, and packaging them up into a single cNum variable.

    4. What does the line "complexop2 = output" exactly do? The function Complexop2 does not explicitly declare what data type it returns (poor practice, IMHO) so it defaults to Variant. So it can return any data type. In this case, it is being assigned the variable output, an array with two elements of type Double.
    Last edited by 6StringJazzer; 04-08-2010 at 03:03 PM.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    04-08-2010
    Location
    Montreal
    MS-Off Ver
    2003 & 2010
    Posts
    16

    Re: Learning VBA: Need Some Explanations on Language

    Thank you for your prompt answers.

    Quote Originally Posted by 6StringJazzer View Post
    I am assuming you made a typing error and this is actually Set_cNum. It is a function call. When you call a function, the call is replaced at runtime by the value returned by the function.
    I am also confused by this, because the code is actually written like this in the book. I assume the authors made a mistake.

    You really helped me out.

    Regards!

  6. #6
    Registered User
    Join Date
    04-08-2010
    Location
    Montreal
    MS-Off Ver
    2003 & 2010
    Posts
    16

    Re: Learning VBA: Need Some Explanations on Language

    I have another doubt:

    Please Login or Register  to view this content.


    What is Run?

    Thanks!

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,725

    Re: Learning VBA: Need Some Explanations on Language

    It is a function. I don't like to give an "I don't know" answer but in this case I could not find a built-in function called Run so I can only assume it is a function defined somewhere else in the code you are looking at.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Learning VBA: Need Some Explanations on Language

    Run is an application method, and is incorrect in the context you show. If it's the name of a function, it shouldn't be.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,725

    Re: Learning VBA: Need Some Explanations on Language

    Quote Originally Posted by shg View Post
    Run is an application method, and is incorrect in the context you show. If it's the name of a function, it shouldn't be.
    I was unfamiliar with Application.Run but after I looked it up I'm convinced that what is intended here. It will run the macro whose name is set in fname and pass in a or b (as appropriate) as a parameter.

    fname itself is passed into BisMet as a parameter. So the code calling BisMet is deciding at runtime what function that BisMet should call. I don't know how BisMet is called so I can't shed any additional light on what is really going on.

    This is a level of indirection I don't recommend. It's just a little too clever, IMHO. It is confusing just to look at the code statically and figure out how it works, and it is doubly confusing to diagnose a problem at runtime. This is what we used to call "control coupling" (not a good kind of coupling). People do stuff like this in C with procedure pointers but it gets tangled quickly.

  10. #10
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Learning VBA: Need Some Explanations on Language

    Agimcomas,

    Training / Books / Sites:

    How to Learn to Write Macros
    http://articles.excelyogi.com/playin...ba/2008/10/27/

    How to use the macro recorder
    http://articles.excelyogi.com/

    Click here and scroll down to Getting Started with VBA.
    http://www.datapigtechnologies.com/ExcelMain.htm

    If you are serious about learning VBA try
    http://www.add-ins.com/vbhelp.htm

    Excel Tutorials and Tips - VBA - macros - training
    http://www.mrexcel.com/articles.shtml

    See David McRitchie's site if you just started with VBA
    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    Debra Dalgleish has some notes how to implement macros here:
    http://www.contextures.com/xlvba01.html

    David McRitchie has an intro to macros:
    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    Ron de Bruin's intro to macros:
    http://www.rondebruin.nl/code.htm

    Creating custom functions
    http://office.microsoft.com/en-us/ex...117011033.aspx

    Writing Your First VBA Function in Excel
    http://www.exceltip.com/st/Writing_Y...Excel/631.html

    http://www.excel-vba.com/
    http://www.mvps.org/dmcritchie/excel/getstarted.htm
    http://www.exceltip.com/excel_links.html

    (livelessons video)
    Excel VBA and Macros with MrExcel
    ISBN: 0-7897-3938-0
    http://www.amazon.com/Excel-Macros-M...7936479&sr=1-1

    http://www.xl-central.com/index.html

    http://www.datapigtechnologies.com/ExcelMain.htm

    Dependent validation lists. Debra has a neat little tutorial here.
    http://www.contextures.com/xlDataVal02.html

    Cascading queries
    http://www.tushar-mehta.com/excel/ne...ing_dropdowns/

    http://www.contextures.com/xlDataVal05.html

    Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
    This page describes how to write code that modifies or reads other VBA code.
    http://www.cpearson.com/Excel/vbe.aspx

    Locating files containing VBA
    Searching Files in Subfolders for VBA code string:
    http://www.dailydoseofexcel.com/arch...a-code-string/

    http://www.pcreview.co.uk/forums/thread-978054.php

    Excel 2003 Power Programming with VBA, by John Walkenbach

    VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

    Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

    VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

    Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…
    http://www.mrexcel.com/learnexcel2.shtml

    DonkeyOte: My Recommended Reading:

    Volatility
    http://www.decisionmodels.com/calcsecretsi.htm

    Sumproduct
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html

    Arrays
    http://www.xtremevbtalk.com/showthread.php?t=296012

    Pivot Intro
    http://peltiertech.com/Excel/Pivots/pivotstart.htm

    Email from XL - VBA
    http://www.rondebruin.nl/sendmail.htm

    Outlook VBA
    http://www.outlookcode.com/article.aspx?ID=40

    Function Dictionary
    http://www.xlfdic.com/

    Function Translations
    http://www.piuha.fi/excel-function-name-translation/

    Dynamic Named Ranges
    http://www.contextures.com/xlNames01.html

    And, as your skills increase, try answering posts on sites like:
    http://www.mrexcel.com/
    http://www.excelforum.com/
    http://www.ozgrid.com/
    http://www.vbaexpress.com/portal.php
    http://p2p.wrox.com/excel-vba-79/
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1