#VALUE error for ranges in subprograms

MODELXLS

New Member
Joined
Aug 14, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have an error when handling ranges. I have a function that returns a calculated value, like this

public function varcalc(a as range, b as range) as double
'description:
'read range a
'perform calculations
'write partial results to range b
'calculate variance and return as varcalc
end function

The function works very well when called from a macro. However, I also need to use it in a cell by calling

=varcalc(selected range a, selected range b)

The functions reads the range a properly but comes with the #VALUE error upon writing to range b.

I cannot figure out where is the problem, maybe with the scope or permissions? Any help would be appreciated. Thank you
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,216
Office Version
  1. 365
Platform
  1. Windows
You can't use a UDF to write to a range.
 

MODELXLS

New Member
Joined
Aug 14, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Thank you Norie for such a quick reply. What you wrote may indicate a significant limitation of Excel programming. The reason of using the ranges in the first place was that the native functions MMULT and MINVERSE, which I employed, work on ranges. I am a new convert from a high-level language and am straggling with the limitation of Excel for numerical modeling.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,914
Office Version
  1. 365
Platform
  1. Windows
I don't think there is any limitation here, I think you are just using the wrong tool.
UDFs are link any other native Excel functions, they can only return a value.
If you want to do something to affect a range, you would not use a Function (UDF), you would use a Sub Procedure instead (sometimes called a "Macro" by people).
 

MODELXLS

New Member
Joined
Aug 14, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Thank you Joe4. You are right. I have already verified it works from a macro, as I stated in my original question. The reason I wanted to call also as =UDF was that I was using it in the SOLVER, which needs a physical cell on a worksheet.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,914
Office Version
  1. 365
Platform
  1. Windows
Note that you can call UDFs from Procedures (Macros).
So you can use them in combintaion with each other.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,363
Messages
5,595,702
Members
414,013
Latest member
tnobbs

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
Top