Make code available as a function

  • Thread starter Thread starter bris
  • Start date Start date
B

bris

Hi

Is there a way to add the macro code so it will be available as a "normal"
function in excel??

Br.
Brian
 
Hi Brian
something like
public function foo()
foo = "some value"
end function

enter =FOO() in a cell in your workbook
 
Instead of Sub ...() use Function :-

'------------------------------------------
Function MyFunction(arguments separated by commas)
'- your code here
'- return value same name as function
MyFunction = x
End Function
'-------------------------------------------

in worksheet use =MyFunction(with arguments).
eg.

'-----------------------------------
Function MyAddition(a,b)
Myaddition = a + b
End Function
'-----------------------------------
 
Make sure you put your code in a general/standard module (in the vbe, Insert
=> Module) rather than a sheet or the thisworkbook module.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top