VBA - Convert all INDIRECT into standard reference

AmFa

New Member
Joined
Nov 7, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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))

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!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top