Solved

User Defined Functions in Excel VBA

Posted on 2011-03-11
6
330 Views
Last Modified: 2012-05-11
Can someone explain to me why User Defined Functions must be in a new module and not in the VBA window for a worksheet or "This Workbook"
0
Comment
Question by:BBlu
  • 3
  • 2
6 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 250 total points
ID: 35112665
Because that's how Microsoft decided they should work. There really isn't a logical reason other than worksheet and workbook code modules are more like class objects versus plain code modules.

Kevin
0
 

Author Comment

by:BBlu
ID: 35113034
Got it.  Thanks, Kevin.
0
 

Author Closing Comment

by:BBlu
ID: 35113037
Thanks, again, Kevin.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 81

Expert Comment

by:byundt
ID: 35113569
I realize that the question has been answered, but thought it worthwhile to elaborate.

You can indeed put user-defined functions in a worksheet code pane or ThisWorkbook. You just cannot use them in worksheet formulas.

If you use them elsewhere on the same module sheet, you call them the same as if they were on a regular module sheet. But if you use them on a different module sheet (either code pane or regular module sheet), then you need to prefix them with the object:
MsgBox Sheet1.Test(12)               'Refers to the Test function on the Sheet1 code pane. Note that Sheet1 is the code name of that worksheet.
MsgBox Worksheets("Tango").Test(12)         'Refers to the exact same function. The worksheet has a tab name of Tango.
MsgBox ThisWorkbook.TestW(12)    'Refers to the TestW function in the ThisWorkbook code pane

Because explicitly mentioning the parent object is somewhat tedious, it is simpler to install your user-defined functions in a regular module sheet.

Brad
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 35113850
BBlu,

To clarify what Brad has posted, you can place code in any code module: worksheet, workbook, class, and general. And you can call that code from anywhere.

However, you asked about a User Defined Function or UDF which, sorry brad, can ONLY be placed in a general code module.

Placing that code in any other code module renders it just plain old code and it is no longer a User Defined Function.

A User Defined Function is, by definition, a function with zero or more parameters that can be referenced in a cell formula.

Kevin
0
 

Author Comment

by:BBlu
ID: 35114592
Thanks to you both.  I appreciate the extra effort all of you put into helping newbies like me.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel formula Sumif not working 4 28
Excel Cell Total 3 22
Formula Help 3 23
Excel - remove duplicates 1 13
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question