Index and match VBA

sysuserwindows

New Member
Joined
Jan 16, 2022
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
I have the table below, but when using the code VBA to search for a value as in below, it appears No match found

Planning.xlsm
ABC
1REFMatQTY
231000260025039501500
3910077B60025039153000
4910022600250392216000
591007760025039311325000
691009960025039002000
7910055600250390050000
82000000000995560025039201500
9200000000019706002503930200
102000000000852060025035551000
Sheet2


VBA Code:
Function FindQTY(REF As Variant, Mat As String) As Long
    Dim ws As Worksheet

    Dim result As Variant
   
    ' Set the worksheet object
    Set ws = ThisWorkbook.Worksheets("SumShet")
   
   
    ' Evaluate the formula and return the result
    result = Application.Evaluate("INDEX('" & ws.Name & "'!C:C, MATCH(1, ('" & ws.Name & "'!A:A=""" & REF & """)*('" & ws.Name & "'!B:B=" & Mat & "), 0))")   
   
    ' Check if the result is an error
    If IsError(result) Then
       MsgBox "No match found"
    Else

        FindQTY = result
    End If
   
End Function

Sub callFindQTY()
    Dim arg1 As Variant
    Dim arg2 As String
   
    arg1 = "910022"
    arg2 = "6002503922"
    Debug.Print FindQTY(arg1, arg2)
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi,
Following Evaluate seems to function as expected
VBA Code:
Function FindQTY(arg1, arg2) As Double
Dim result As Variant
    ' Evaluate the formula and return the result
    result = Evaluate("=INDEX(C1:C10,MATCH(" & arg1 & " & " & arg2 & ",A1:A10&B1:B10,0))")
    ' Check if the result is an error
    If IsError(result) Then
       MsgBox "No match found"
    Else
        FindQTY = result
    End If
End Function

Sub callFindQTY()
Dim arg1, arg2
    arg1 = "910022"
    arg2 = "6002503922"
    Debug.Print FindQTY(arg1, arg2)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,576
Members
448,972
Latest member
Shantanu2024

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