简体   繁体   English

用非易失性VBA方法代替间接方法,但是不能使用Excel函数(如OFFSET)嵌套返回值?

[英]Replacing Indirect with non-volatile VBA method, but cannot nest the returned value with Excel functions like OFFSET?

I have a follow-up question to this question . 对此问题有一个后续问题

So I want to replace INDIRECT function calls with a VBA method because INDIRECT is volatile and my excel doc is taking several seconds to load and sometimes is not responding. 所以我想用VBA方法替换INDIRECT函数调用,因为INDIRECT易失,并且我的excel文档需要花费几秒钟来加载,有时甚至没有响应。

But when I use the INDIRECTVBA method and nest it with an OFFSET function I get an error and it shows "#VALUE!" 但是,当我使用INDIRECTVBA方法并将其与OFFSET函数嵌套时,会出现错误,并显示“ #VALUE!”。 (yes I know OFFSET is another volatile function, I will replace with INDEX..) (是的,我知道OFFSET是另一个易失函数,我将替换为INDEX。)

Specifically: Cell BJ10 contains the text "$R$71" which is a reference to my cell holding the data. 具体来说:单元格BJ10包含文本“ $ R $ 71”,这是对保存数据的单元格的引用。

=INDIRECT($BJ$10) works but is volatile. = INDIRECT($ BJ $ 10)可以工作,但易失。

=INDIRECTVBA($BJ$10) works. = INDIRECTVBA($ BJ $ 10)起作用。

=(OFFSET(INDIRECT($BJ$10),0,0)) works but is doubly volatile. =(OFFSET(INDIRECT($ BJ $ 10),0,0))可以工作,但易失。

=(OFFSET(INDIRECTVBA($BJ$10),0,0)) does not calculate, it shows "#VALUE!" =(OFFSET(INDIRECTVBA($ BJ $ 10),0,0))不计算,它显示“ #VALUE!”

Any thoughts? 有什么想法吗?

Here is the INDIRECTVBA method: 这是INDIRECTVBA方法:

Public Function INDIRECTVBA(ref_text As String)
    INDIRECTVBA = Range(ref_text)
End Function

Public Sub FullCalc()
    Application.CalculateFull
End Sub

Your function doesn't return a range, so it fails as the first argument to OFFSET() (which requires a range in that position). 您的函数不返回范围,因此它作为OFFSET()的第一个参数(在该位置需要一个范围)而失败。

Also, your function will fail when any other sheet is active (assuming it's in a standard module), because the scope of Range() defaults to the ActiveSheet. 同样,当其他任何工作表处于活动状态时(假设它在标准模块中),您的函数也会失败,因为Range()Range()默认为ActiveSheet。

Try something like: 尝试类似:

Public Function INDIRECTVBA(ref_text As String)
    Set INDIRECTVBA = Application.ThisCell.Parent.Range(ref_text)
End Function

If everything's not on the same sheet then you will need some way to specify which sheet should be used in your UDF 如果所有内容不在同一张纸上,那么您将需要某种方法来指定在UDF中应使用哪张纸

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

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