Solved

User Defined Functions in Excel VBA

Posted on 2011-03-11
6
328 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 80

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
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 …

707 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

20 Experts available now in Live!

Get 1:1 Help Now