Using Index and Match to look for two criterias

iefraim

New Member
Joined
Feb 23, 2016
Messages
2
Hello Everyone! Hope you all are fine.

First, I want to apologize if there's already a thread about this subject, I searched a lot and didn't found anything that could help me with my problem.

So, I'm trying to use VBA userform to look for two criterias from my worksheet.

A1A2A3
ProductFlavorPrice
Ice CreamChocolate5 BRL
Ice CreamStrawberry6 BRL
PopsicleGrape2 BRL
PopsicleVanilla2,50 BRL

<tbody>
</tbody>

So, what I'm trying to do is a userform that look for both information (Product and Flavor) typed by the customer, like this (It's in portuguese, but it won't confuse you):

2781_1456279522.JPG

But when I type to search any product/flavor that ISN'T the first item of the list, it doesn't find and says that it was unable to get the Index property...

I'm using this code:

Code:
Private Sub CommandButton1_Click()
Dim produto As String
Dim Sabor As String


produto = TextBox1.Text
Sabor = TextBox2.Text


TextBox3 = "R$" & Application.WorksheetFunction.Index(Range("C2:C5"), _
Application.WorksheetFunction.Match(produto, Range("A2:A5"), 0), _
Application.WorksheetFunction.Match(Sabor, Range("B2:B5"), 0))
End Sub

Thanks in advance.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
you could use an Excel formula to evaluate the answer, and just pass the result to your form. With your data in cells A1:C4, and the 2 criteria in cells A6, B6, then an array formula (entered using Shift + Ctrl + Enter, not just Enter) will do this: =INDEX(C1:C4,MATCH(A6&B6,A1:A4&B1:B4,0))
 
Upvote 0
Wow man, it's a great ideia, but is there a way to make it without having to evaluate? I used this workbook as an example, but in the other one, I'll find more than one thing and put to a textbot to show, than I'll "dim" them to make my macro be faster.

I appreciate your answer and it's a very good idea, I can use it, but first I want to know if this is the only way to get the multi-criteria index-match, 'cause the userform will look for more than one information based on the things the user typed, and evaluate every single one will make the code even bigger I guess.

Thank you, man.
 
Upvote 0
With Excel & VBA there is never only one way. You can usually use many combinations of formula and code, but your aim should be to find the most efficient and simple way to do it. For one pair of products I think a formula will be easier than code and so I suggested that. However a VBA approach would also be possible

This approach "probably" works but I haven't tested it. I've created a function that returns a text string, and which is called by your command button. I've done it this way because you can easily re-use this approach in other places

The function needs two text strings to be passed into it, and returns a new text string if both criteria are met, or "" if none found. You'll need to name your range of data, I've called it "myPrices"
Code:
Option Explicit

Private Sub CommandButton1_Click()
TextBox3 = getValue(TextBox1.Text, TextBox2.Text)
End Sub

Function getValue(str1 As String, str2 As String) As String
Dim cl As Range
For Each cl In Range("myPrices").Columns(1).Cells
    If cl = str1 Then
        If cl.Offset(0, 1) = str2 Then
            getValue = "R$ " & cl.Offset(0, 2)
            Exit Function
        End If
    End If
Next cl

End Function
 
Upvote 0

Forum statistics

Threads
1,217,332
Messages
6,135,947
Members
449,974
Latest member
riffburn

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