<Out of Context> Error thrown in VB Basic User Defined Function

grs

New Member
Joined
Mar 12, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I am simply attempting to set the value of a cell using a function I've written in visual basic. Reading values from the cell all work correctly (second image below) but setting the Value property continually throws an <Out of Context> error in the watch window. I've not had this problem previously in code I've written in earlier versions of Excel and am stumped.

In the first image the code is being called in cell B63 and is attempting to reset the value of cell B60.

In the second image the code has been called and you can see the values of the local variables in the Locals window along with the value of the B60 cell in the Watches window.

In the third image I've stepped through the line that should set the value of Cell B60 to 2 and receive the <Out of Context> error.

Any help would be appreciated.

1678674560179.png


1678674993140.png


1678675153357.png
 

Attachments

  • 1678674676083.png
    1678674676083.png
    101.8 KB · Views: 4

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I don't suppose the sheet error message says something like "a value used in the expression is the wrong data type"? In that case, perhaps caused by your function returning a string but your value of 2 is numeric (".Value = 2). I'd start by declaring the function return type as variant, or perhaps integer or long. However, variant shouldn't fail if the return value is ever a string when you expect a number, or is null or zls, but declaring return type as integer or long probably will fail in those cases.
 
Upvote 0
The error message is shown in the Watches area in the 3rd image. The function is returning a string (last line of code sets ClearValue to a string) but execution of the code does not get this far as the function fails on the line that is attempting to set the Value property equal to 2 and does not execute any line beyond that point.
 
Upvote 0
A UDF cannot change the value of another cell.
 
Upvote 1
Solution
A UDF in a cell cannot alter the value of a different cell. (there are workarounds but it is, in my opinion, a bad idea as it makes your workbook extremely difficult to audit/maintain)
 
Upvote 1

Forum statistics

Threads
1,214,566
Messages
6,120,257
Members
448,952
Latest member
kjurney

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