2
I wonder how I can use an excel sheet as a function.
Let's say I have a complex calculation in Sheet1 with one parameter I would like to iterate over a range like [1 to 400] in a second sheet to create an x, f(x) table in Sheet2.
I'll be using Excel or Open Office.
How would I go about doing this?
Thanks.
Progress update
Attempted to do a test in the Sheet (General):
Public Function test() as Boolean
test = True
End Function
Then in a cell:
=test()
Resulting in #Name?
Also tried scouering online tutorials.
Partially solved:
- Visibility of the function is achieved by putting the code in a module.
- Insert -> module in VBA.
Progress:
Setting av value using
Worksheets("Sheet1").Range("A1").Value = 10
or
Worksheets("Sheet1").Cells(1,1).Value = 10
failes silently and doesn't execute the line under.
Progress:
The techniques work fine if I dumb down the code to a parameterless subroutine and assign it to a button.
Workaround
Implementation was really straight forward when implementing using a VBA macro launched from a button rather than using a function.
I iterated over a range of rows to get the inputs to the calculations.
1RE the test function failing - have you enabled macros for the file in question? IIRC, with them disabled, custom functions will not operate. – DMA57361 – 2010-09-07T13:04:09.873
Thanks, I think maybe it was a visibility issue. Moving the code to a module solved it. Now on to the hard part :) – tovare – 2010-09-07T13:06:18.927
1Both those examples in your last update work for me (Excel 2003, what version are you using?). You can also use
Sheets("Sheet1")
and.Formula = 10
- both effectively the same when setting values (but Formula is quite different when retrieving). I doubt this will help though... – DMA57361 – 2010-09-07T14:33:51.090I tried two installations of Excel 2003. One had some VB-buttons disabled by the corporate admin, but I got around it. The second should be pretty unconstrained by coorporate snake oil, still didn't work though. I'll try some more tomorrow :-) – tovare – 2010-09-07T19:16:18.303
1If the sheet is protected then you can't set the value. You either have to unprotect it, or user the USERINTERFACEONLY parameter when locking it. – Lance Roberts – 2010-09-07T20:28:28.680
The sheet isn't protected. The funny part, changing the code to a sub and assigning it to a button works. – tovare – 2010-09-08T08:26:59.903
Related (?): How do I add VBA in MS Office?
– Scott – 2019-02-20T05:40:49.337