Is this valid???????

EDrone

New Member
Joined
Sep 10, 2002
Messages
4
I'm rather new to Excel programming, but I'm having problems doing what appears to something very simple. I want a formula to return the value of a calculation, AND set some values in some other cells. I've simplified down to two lines of code and the second assignment statement is causing me grief when I type the following in Cell A1
=MyFunc()

Function MyFunc() as Integer
MyFunc = 1
ActiveSheet.Range("A5").Value = 1
End Function

Error Returned :
Error 1004
Application-defined or object-defined error

Is this not valid? Or am I missing the boat on what you can do in Formulas?

This code works great if it was Sub, but I also can't figure out how to run the Sub as a formula. It appears Subs can only be run from menus, controls, etc... or through code.

Thanks for any help I can get.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Function MyFunc() as Integer
MyFunc = 1
ActiveSheet.Range("A5").Value = 1
End Function

It should be:
Function MyFunc() As Integer
MyFunc = ActiveSheet.Range("A5").Value
End Function
 
Upvote 0
Function procedures used in worksheet formulas cannot manipulate ranges. They can only return a result. So:

ActiveSheet.Range("A5").Value = 1

will result in an error.

What is the relationship between the result of your function and the data in the other cells? Define it and use formulas in the other cells to reference the cell containing your function.
 
Upvote 0
Emily and Andrew. Thanks for your quick response and help.

Andrew,
I was afraid you would say that you couldn't manipulate a Range within a Function. My original goal was to have a function with multiple parameters (Cell references). The function would query a database and dump the results and return the recordcount as the function's return value. I wanted it to be dynamic so that if any of the cells used as parameters changed the recordset would change also. Is there a better way?
 
Upvote 0
I don't really know. Couldn't you use the Sheet_Change event to run your query etc? Test on the cell that's changed using Target.Address.
 
Upvote 0

Forum statistics

Threads
1,224,352
Messages
6,178,069
Members
452,822
Latest member
MtC

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