简体   繁体   English

返回值将覆盖自身的Excel VBA UDF

[英]Excel VBA UDF that return value will override itself

Can I write a UDF in Excel VBA where the return value from the function will override the cell value from it is called from? 我可以在Excel VBA中编写UDF的功能,该函数的返回值将覆盖调用它的单元格值吗?

The function get information with a sql request. 该函数通过sql请求获取信息。 In this case it's only master data for example the item description. 在这种情况下,它仅是主数据,例如项目描述。 If the user will use this function in a worksheet in many cells excel will recalculate the cell value every time you change something. 如果用户将在许多单元格的工作表中使用此功能,则每次更改内容时,excel都会重新计算单元格值。 This has poor performance and normally it's only necessary to get the information one time and it hasn't to be updated in this case. 这种方法的性能很差,通常只需要一次获取信息,在这种情况下就不必更新。

I thought to use application.caller.address method to get the address the function was called from but it seems it can't set the cell value for this address within the function. 我以为可以使用application.caller.address方法来获取调用该函数的地址,但似乎无法在函数内为此地址设置单元格值。 So the return value of the function should override the original formula that run the function. 因此,该函数的返回值应覆盖运行该函数的原始公式。

Is this possible 这可能吗

thanks for your help 谢谢你的帮助

No. 没有。

As you may have noticed Excel cells have multiple layers. 您可能已经注意到Excel单元格具有多层。

One is the "value". 一种是“价值”。 Another one the formula you can assign. 您可以分配的另一个公式。

A funtions returns a value, therefore the return value only accesses this layer. 函数返回一个值,因此返回值仅访问此层。 So you cannot return a replacement for the formula cause it is on another layer. 因此,您无法返回公式的替换项,因为它位于另一层。

A function differs from a sub in the return value, a sub does not return anything. 函数与子函数的返回值不同,子函数不返回任何内容。 Due to your behaviour of "one time usage" a sub will fit your need more than a function, because you dont want to return a value but to remove or replace certain content from cell (the formula). 由于您具有“一次性使用”的行为,因此子函数比函数更适合您的需求,因为您不想返回值,而是要删除或替换单元格(公式)中的某些内容。

However, this does not mean you cannot do this with a function - but still not with a return value. 但是,这并不意味着您不能使用函数来执行此操作-但仍不能使用返回值。 But you need to rewrite the whole formula on a data refresh if you would use such a function. 但是,如果要使用这样的函数,则需要在数据刷新时重写整个公式。

You may have missed a point that you make you laugh yourself I guess. 我想你可能错过了一个让自己笑的观点。 Excel has such a thing natively. Excel本身具有这种功能。 But it is not a function. 但这不是功能。

Copy your cells and paste them but use "values only". 复制单元格并粘贴它们,但使用“仅值”。

Totally has the same effect. 完全具有相同的效果。

Also in terms of recalculation... why not turn it off? 同样在重新计算方面...为什么不将其关闭? This would you not make to rewrite the function each time. 您不必每次都重写该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM