简体   繁体   中英

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.

But when I use the INDIRECTVBA method and nest it with an OFFSET function I get an error and it shows "#VALUE!" (yes I know OFFSET is another volatile function, I will replace with INDEX..)

Specifically: Cell BJ10 contains the text "$R$71" which is a reference to my cell holding the data.

=INDIRECT($BJ$10) works but is volatile.

=INDIRECTVBA($BJ$10) works.

=(OFFSET(INDIRECT($BJ$10),0,0)) works but is doubly volatile.

=(OFFSET(INDIRECTVBA($BJ$10),0,0)) does not calculate, it shows "#VALUE!"

Any thoughts?

Here is the INDIRECTVBA method:

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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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