Black Orchid
Black Orchid

Reputation: 121

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

Upvotes: 0

Views: 1094

Answers (1)

Tim Williams
Tim Williams

Reputation: 166835

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

Upvotes: 3

Related Questions