Very simple question on functions

buffalo

Board Regular
Joined
Jun 19, 2003
Messages
183
Hi,

I have still not figured out where to put the VBA code for functions in excel.

I click Alt + F11 and get to the VBA code part, I can write the Sub's and when i click the Run button they run well.

But not so for functions. ONLY when I open a new 'Module' and write my function in that, then i can work the fucntion from the worksheet.

QUESTIONS:
....Where is the right place to write fucntions?
....What is module useful for?

thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I get the same thing with Alt F11.

To write a function, you can choose Tools...macro....macros.....then type in a macro name, click create and change the sub to a function.

Not very elegant but it works.
 
Upvote 0
buffalo said:
Hi,

I have still not figured out where to put the VBA code for functions in excel.

I click Alt + F11 and get to the VBA code part, I can write the Sub's and when i click the Run button they run well.

But not so for functions. ONLY when I open a new 'Module' and write my function in that, then i can work the fucntion from the worksheet.

QUESTIONS:
....Where is the right place to write fucntions?
....What is module useful for?

thanks

I guess the first thing you need to understand is what a module is. Think of a module as a container. There are worksheet modules, the ThisWorkbook module, and then the...um....module modules. I would recommend that you use the worksheet and ThisWorkbook modules for EVENT macros (macros that automatically run when a particular event occurs.) If you want a macro or function to be available to the entire workbook, you will put it in a module module. You might ask, "Why not put it in the ThisWorkbook module if you want code to be run anywhere in the workbook?" The ThisWorkbook module is designed for workbook events. The ThisWorkbook is something specific to Excel VBA. Regular VB wouldn't have a ThisWorkbook sort of thing. It would have the module modules though.

Did I make this more confusing than it needs to be?
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top