Solved

User Defined Functions in Excel VBA

Posted on 2011-03-11
6
331 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

839 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