Solved

User Defined Functions in Excel VBA

Posted on 2011-03-11
6
329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now