Hi,
I'd like to convert an indirect formula to the direct link. I have found various VBA codes which work fine for "simple" indirect formulas, like here from Andre: Convert indirect function to direct link
I'm wondering if it's possible to adapt the following code so that it would work for formulas that have indirect references within indirect references. like here:
=INDIRECT("'["&$B21&"]"&$C21&"'!"&XLOOKUP(MATCH($D21,INDIRECT("'["&$B21&"]"&$C21&"'!10:10"),0),$AD:$AD,$AE:$AE)&MATCH(INDIRECT(O$18&"19"),INDIRECT("'["&$B21&"]"&$C21&"'!R:R"),0))
When using the code above, it seems that it runs into a Type Mismatch error at line 22:
cformula = Mid(cformula, 1, posOf - 1) & eval & Mid(cformula, posOf + endOf + 9 + 1)
I realise it's a very convoluted query for a very convoluted formula, but if someone could help that would be great.
Thank you!
I'd like to convert an indirect formula to the direct link. I have found various VBA codes which work fine for "simple" indirect formulas, like here from Andre: Convert indirect function to direct link
I'm wondering if it's possible to adapt the following code so that it would work for formulas that have indirect references within indirect references. like here:
=INDIRECT("'["&$B21&"]"&$C21&"'!"&XLOOKUP(MATCH($D21,INDIRECT("'["&$B21&"]"&$C21&"'!10:10"),0),$AD:$AD,$AE:$AE)&MATCH(INDIRECT(O$18&"19"),INDIRECT("'["&$B21&"]"&$C21&"'!R:R"),0))
VBA Code:
Sub convert_indirect()
Dim posOf, endOf, nestLevel As Integer
Dim eval, cformula As String
Dim r, c As Range
Set r = Selection
For Each c In r
cformula = c.formula
posOf = InStr(1, cformula, "INDIRECT(", vbTextCompare)
If (posOf > 0) Then
endOf = 1
nestLevel = 1
Do While nestLevel > 0
Select Case Mid(cformula, posOf + 10 + endOf, 1)
Case "("
nestLevel = nestLevel + 1
Case ")"
nestLevel = nestLevel - 1
End Select
endOf = endOf + 1
Loop
eval = Evaluate("=" & Mid(c.formula, posOf + 9, endOf))
cformula = Mid(cformula, 1, posOf - 1) & eval & Mid(cformula, posOf + endOf + 9 + 1)
c.formula = cformula
End If
Next c
End Sub
When using the code above, it seems that it runs into a Type Mismatch error at line 22:
cformula = Mid(cformula, 1, posOf - 1) & eval & Mid(cformula, posOf + endOf + 9 + 1)
I realise it's a very convoluted query for a very convoluted formula, but if someone could help that would be great.
Thank you!