+ Reply to Thread
Results 1 to 23 of 23

Excel 2007 : User Defined Functions

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Smile User Defined Functions

    In Excel 2007 Inside Out by Mark Dodge it is discussed in Chapter 27 on how to develop User Defined or Custom Functions in VBA in this case using the DISCOUNT Function. I am prepping for the Excel 2007 Expert Exam and need help in this area. How do I get the code right in VBA when developing user defined or custom functions? Do macros and custom functions come together as one code?

  2. #2
    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: User Defined Functions

    I don't understand your question. Do you know how to write a UDF?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    Well I know that in VBA the code begins with Function and End Function. Can it be any function? There is code that uses IF, else and Then statements. Its Chapter 27 in Excel Inside Out 2007.

  4. #4
    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: User Defined Functions

    Yes, you can write any function you want in VBA:

    Please Login or Register  to view this content.
    =AddEm(1, 3.5)

  5. #5
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    Now once you add this code in VBA do you have to save the module in order for it to work in Excel? Can a macro be combined within this code?

  6. #6
    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: User Defined Functions

    UDFs in a workbook are stored in a standard code module, which is (are) part of the workbook. Other code can be in a sheet module or the ThisWorkbook module, or in a forms module.

    My working definition of a macro is a VBA sub that takes no arguments, i.e., one that can be selected and run from the macros dialog (via Alt+F8).

    For a basic place to start, google introduction to excel vba
    Last edited by shg; 01-17-2012 at 09:39 PM.

  7. #7
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    I'm just not clear with any of it as it relates to VBA and custom functions. Google help does not help me. None of it makes any sense in VBA. Maybe its better if there wasn't VBA or no code.

  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: User Defined Functions

    VBA is the native automation language for Office -- it's what you use to write macros, UDFs, ...

  9. #9
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    I'm just simply frustrated with VBA. I have no prior knowledge of how it all works. I'm frustrated with macros and custom functions and no online training has helped.

  10. #10
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    I have no clue.

  11. #11
    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: User Defined Functions

    I think any basic book would help you get started. I've used Walkenbach's VBA for Dummies for a class -- it wasn't bad.

    Just start writing code and ask questions when you get stuck.

  12. #12
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: User Defined Functions

    Its all in the book, even the VBA required to copy

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    What I don't understand is why some of this code appears automatically when it should not and compile errors happen when you enter a first letter. VBA code access denied. No VBA code makes since with custom functions. I even had to turn to Excel 2010 Formulas and Functions Inside Out Chapter 6 and it has no since to any explanation.

  14. #14
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    The actual code makes no since. There should be no Function and End Function, thats makes no since with it popping up automatically. Why is it indented anyway and you could leave it all in massive functions in Excel.

  15. #15
    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: User Defined Functions

    There should be no Function and End Function, thats makes no since with it popping up automatically.
    Why not?

    Have you never seen any programming language?

    Why is it indented anyway ...
    Indenting is purely cosmetic to improve readability.

    ... and you could leave it all in massive functions in Excel.
    For this simple example, you certainly could.
    Last edited by shg; 01-18-2012 at 07:36 PM.

  16. #16
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    I don't have a clue.

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: User Defined Functions

    Quote Originally Posted by Rblack40 View Post
    I am prepping for the Excel 2007 Expert Exam and need help in this area.
    Who is prepping you for this exam? You're enrolled in a course somewhere? Based on your questions and frustrations, your comfort level with Excel and VBA terms is below Novice level, so an Expert Exam would be premature, surely.

    Your course materials should walk you through learning these things slowly, step by step. It sounds like you're diving in all at once trying to absorb the entire environment and getting frustrated by it. I remember that's how I felt when I was first using the VBEditor.

    But your frustration will subside over time as you spend more time in there working with more and more examples from your course materials. This is going to be a long process to get to the point you're ready for an expert's exam, so relax and keep working at it.


    Here, we are more suited to assist you in understanding a specific thing, Ask a specific question (like you started to do originally). "I have no clue"... that's just you frustrating yourself. Give it time.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  18. #18
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    I have the Excel Expert 2007 exam objectives in front of me from Certiport and have been training through several online sites including Lynda.com, Office Online and have severaal Microsoft books in Excel. The VBA code for Macros and Custom Functions is giving me alot of trouble because the code itself makes no logical since. Its just not clicking in my mind. I'm not seeing why you need a Function and then an End Function and then other code inbetween VBA code. I am no programmer and have no prior knowledge of programming.

  19. #19
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: User Defined Functions

    Hi RBlack40 - let me jump in here...
    Reading the code given above - It is a User Defined Function called Discount. It takes two parameters or arguments to make it work. These are the quantity and price values. You know it is a function because it says "Function" instead of "Sub". Functions return stuff and Subs don't return anything. You need to have "Discount = something" later in the UDF to know what is returned.

    The code starts and says if the quantity is greater than or equal to 100 then give the customer a discount. If that fails then the discount is zero. The amount of the discount is the quantity times the price times .1. It looks like this function gives a discount of 10% if the customer buys 100 or more of something.

    The way you then use this FUNCTION in excel is to put something like this is a single Cell.
    = Discount(200, 4.50)
    press enter after putting this in a cell and see if it calculates correctly. then change the 200 to 50 in the above to see if it returns zero.

    You can also put cell ranges in a UDF. You could have 200 in A1 and 4.50 in B1 and you could do...
    =Discount(A1,B1)
    This would give you the same answer as above..

    User Defined Functions don't recalculate automatically unless you use Application.Volitile in the first line after the function statement.

    I hope the above gets you over some of the learning problems with VBA and UDFs.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  20. #20
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    Are you an Excel MVP by chance? VBA does seem confusing at first glance in both Excel 2007 and 2010.

  21. #21
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: User Defined Functions

    VBA is confusing to you because it's unfamiliar. Remember the first time you sat in a driver's seat? That unfamiliar feeling took some time to get over. But the feeling is irrelevant, ultimately. If you feel you must pass this test then you'll have to sit there and read through the codes and examples and ask questions and digest answers until it starts to click. You just do it.

    There is a structure to writing VBA and some of it is absolute. Every sub or function will start with a declaration of it's name and possible required parameters.... then there will activity code, and the sub will end with a clearly marked "END SUB" or "END FUNCTION". Like a train has an engine in front starting you out and a caboose saying "it's over", and a random number of cars in between that is your activity code.

  22. #22
    Registered User
    Join Date
    01-17-2012
    Location
    Orlando FL
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    22

    Re: User Defined Functions

    Is VBA updated for Excel 2010 or is it virtually the same as in Excel 2007?

  23. #23
    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: User Defined Functions

    It's the same.

+ 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