Forum Discussion

Charlie2295's avatar
Charlie2295
Copper Contributor
Dec 19, 2022

IndexMatch VBA inserting @ sign

Hi,

 

I am currently trying to automate a process in VBA but am encountering a problem when inserting an Index Match formula. When the formula is inserted by the macro a couple of @ signs are inserted which makes the formula error, without them the formula works fine. I understand that this a function of the new version of Excel, but does anyone know how I can avoid them coming into the formula, or another way to get the same result without using the Index Match formula.

 

Background: I am using Index Match to match 2 values in one sheet (x) with 2 values in another sheet (y), and then return the value in the same row in sheet y.

 

This is the formula that works in Excel normally: =INDEX('Flight Estimates'!F:F,MATCH(1,('Flight Estimates'!A:A=AH2)*('Flight Estimates'!B:B=AI2),0))

 

This is what excel outputs with the macro: =INDEX('Flight Estimates'!F:F,MATCH(1,(@'Flight Estimates'!A:A=AI2)*(@'Flight Estimates'!B:B=AH2),0))

 

This is the code that I have put in VBA:

 

Sub LevyAmount

Dim c, r, c1, c2 As Integer

Dim ws, c7, c8 As String

 

ws = "Current Month"

 

    'Find columns

        'From location

                c = 1

            Do Until Worksheets(ws).Cells(1, c).Value = "From Location"

                c = c + 1

            Loop

        'To Location

                c1 = 1

            Do Until Worksheets(ws).Cells(1, c1).Value = "To Location"

                c1 = c1 + 1

            Loop

       'Flight Levy Amount

                c2 = 1

            Do Until Worksheets(ws).Cells(1, c2).Value = "Flight Levy Amount"

                c2 = c2 + 1

            Loop

     

        'Store From column as letter

        c7 = Split(Cells(1, c).Address, "$")(1)

        'Store To column as letter

        c8 = Split(Cells(1, c1).Address, "$")(1)

 

‘Input IndexMatch formula

            r = 2

                Do While Worksheets(ws).Cells(r, 1).Value <> ""

      Worksheets(ws).Cells(r, c2).Formula = "=INDEX('Flight Estimates'!F:F,MATCH(1,('Flight Estimates'!A:A=" + CStr(c7) + "" + CStr(r) + ")*('Flight Estimates'!B:B=" + CStr(c8) + "" + CStr(r) + "),0))"

                         r=r+1

                Loop

End Sub

 

Thanks for any help!

 

5 Replies

Resources