Functions in VBA used in spreadsheet cell

J

JRXPL

I have written an extremely simple function in VBA module for sheet 1 and
when I try to use that function in a cell in sheet 1 I get a *Name? error.
Has anyone seen this before? How do I get the function to work?

Function CalculateOne (a,b)
CalculateOne = 2*a + b
EndFunction
 
D

Dave Peterson

Make sure your function is in a General module--not under the ThisWorkbook
module, not behind a worksheet module.

Make sure you allow macros to run when you open your workbook.

And if you use ranges in your formula, make sure you're spelling the addresses
correctly.

If the problem doesn't go away, then make sure you share what you typed into the
cell.

ps. I'm assuming the typos in the function are just in the post. In the
future, it's better to copy directly from your code and paste into the message.
 
J

JRXPL

Thank you very much-

I checked the Macro Security and set it to low. I typed the following into
the General module, no ranges, checked spelling. It still doesn't work when
I type the below command into the excel cell.

Function CalculateOne(a, b)
CalculateOne = a + b
End Function


=CalculateOne(2,3)




--
Thank you,
JRXPL


Dave Peterson said:
Make sure your function is in a General module--not under the ThisWorkbook
module, not behind a worksheet module.

Make sure you allow macros to run when you open your workbook.

And if you use ranges in your formula, make sure you're spelling the addresses
correctly.

If the problem doesn't go away, then make sure you share what you typed into the
cell.

ps. I'm assuming the typos in the function are just in the post. In the
future, it's better to copy directly from your code and paste into the message.
 
D

Dave Peterson

It worked fine for me.

And you did put it in a General module in the workbook's project that held the
worksheet with the cell with the formula?
Thank you very much-

I checked the Macro Security and set it to low. I typed the following into
the General module, no ranges, checked spelling. It still doesn't work when
I type the below command into the excel cell.

Function CalculateOne(a, b)
CalculateOne = a + b
End Function

=CalculateOne(2,3)
 
J

JRXPL

Thanks for checking it. It is in the general module for the sheet the cell
is in. I even tried typing it in a cell in all sheets. Still not working???
--
Thank you,
JRXPL


Dave Peterson said:
It worked fine for me.

And you did put it in a General module in the workbook's project that held the
worksheet with the cell with the formula?
 
D

Dave Peterson

Try changing the function to:

Function CalculateOneA(a, b)
CalculateOneA = a + b
End Function

=CalculateOneA(2,3)

Maybe you have something that's conflicting with that name--module name
maybe?????

(If you did rename the module, change it to something else--Mod_CalculateOne???)
Thanks for checking it. It is in the general module for the sheet the cell
is in. I even tried typing it in a cell in all sheets. Still not working???
 
J

JRXPL

Thanks-

Did a copy paste. Same result. I did notice that Excel runs a virus scan
when I open the file???
 
G

Gord Dibben

Sheets do not have "general modules".

Workbooks have them.

Sheets have "sheet modules" which Dave instructed you to stay away from.

Remove the function code from the sheet module and place it into a general or
standard module.


Gord Dibben MS Excel MVP
 
D

Dave Peterson

Thanks for reading the response better than I did!

Maybe JRXPL (gesundheit!) will see your message.
 
J

JRXPL

Thanks for the info everyone, reading over the posts, I tried to create a new
Module and inserted the Function code and it works in this manner.

I am unable to get it to work in the code area for (General) or the code
area for Worksheet when I dblclick on the worksheet or the code area for
Workbook when I dblclick on the project.

Sorry for the misunderstanding, but is this the way it is suppose to work? I
feel like I am missing a basic concept. I have tried this on two different
computer systems.

Thanks again.
 
G

Gord Dibben

One more time..................

With your workbook open, hit Alt + F11 to open the Visual Basic Editor window.

CTRL + r to open the Project Explorer.

DO NOT doubleclick on anything!!

Right-click on your workbook(project) and Insert>Module.

Paste this into that module...........

Function CalculateOne(a, b)
CalculateOne = a + b
End Function

File>Save the workbook.

Alt + q to return to Excel window.

In any cell enter =Calculateone(2,3) which returns 5

Make sure you have Tools>Options>Calculation set to "Automatic"


Gord
 
J

JRXPL

Thank you,
JRXPL


Gord Dibben said:
One more time..................

With your workbook open, hit Alt + F11 to open the Visual Basic Editor window.

CTRL + r to open the Project Explorer.

DO NOT doubleclick on anything!!

Right-click on your workbook(project) and Insert>Module.

Paste this into that module...........

Function CalculateOne(a, b)
CalculateOne = a + b
End Function

File>Save the workbook.

Alt + q to return to Excel window.

In any cell enter =Calculateone(2,3) which returns 5

Make sure you have Tools>Options>Calculation set to "Automatic"


Gord
 

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

Top