The Problem
Assume that the active cell contains a formula based on the INDEX
function:
=INDEX(myrange, x,y)
I would like to build a macro that locates the value found value by INDEX
and moves the focus there, that is a macro changing the active cell to:
Range("myrange").Cells(x,y)
Doing the job without macros (slow but it works)
Apart from trivially moving the selection to myrange
and manually counting x
rows y
and columns, one can:
Copy and paste the formula in another cell as follows:
=CELL("address", INDEX(myrange, x,y))
(that shows the address of the cell matched by INDEX).
Copy the result of the formula above.
Hit
F5
,Ctrl-V
,Enter
(paste the copied address in theGoTo
dialog).
You are now located on the very cell found by the INDEX
function.
Now the challenge is to automate these steps (or similar ones) with a macro.
Tentative macros (not working)
Tentative 1
WorksheetFunction.CELL("address", ActiveCell.Formula)
It doesn't work since CELL
for some reason is not part of the members of WorksheetFunction
.
Tentative 2
This method involves parsing the INDEX
-formula.
Sub GoToIndex()
Dim form As String, rng As String, row As String, col As String
form = ActiveCell.Formula
form = Split(form, "(")(1)
rng = Split(form, ",")(0)
row = Split(form, ",")(1)
col = Split(Split(form, ",")(2), ")")(0)
Range(rng).Cells(row, CInt(col)).Select
End Sub
This method actually works, but only for a simple case, where the main INDEX
-formula has no nested subformulas.
Note
Obviously in a real case myrange
, x
and y
can be both simple values, such as =INDEX(A1:D10, 1,1)
, or values returned from complex expressions. Typically x
, y
are the results of a MATCH
function.
EDIT
It was discovered that some solutions do not work when myrange
is located on a sheet different from that hosting =INDEX(myrange ...)
.
They are common practice in financial reporting, where some sheets have the main statements whose entries are recalled from others via an INDEX+MATCH
formula.
Unfortunately it is just when the found value is located on a "far" report out of sight that you need more the jump-to-the-cell function.