Using application.match to find more than one criteria

learningthings

New Member
Joined
Oct 29, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I have a Userform that inputs data to a spreadsheet labelled ("Sales")
Now, sometimes, a sale can be paid for by two methods of payment (i.e. Cash and Credit)
The way I have the system set up is that if there are two methods of payment, the transaction is recorded under the "Sales" tab twice with the same ID number with different methods of payment, once with cash, once with credit

Now, I want to be able to pull up the data from the sheet ("Sales") based on two criteria: the ID and the payment method.
This is what I would usually use - This is able to fill textboxes on the userform based on the sales ID :
VBA Code:
Dim iRow As Long
With Me
On Error Resume Next
iRow = Application.Match(.txtID.Text, Worksheets("Sales").Columns(1), 0)

On Error GoTo 0
If iRow > 0 Then
frmSales.txtitem.Text = Worksheets("Sales").Cells(iRow, "B").Value
.frmSales.txtquantity.Text = Worksheets("Sales").Cells(iRow, "D").Value
frmSales.txttotal.Text = Worksheets("Sales").Cells(iRow, "C").Value
End If
This gives me the first result matching the sales ID. But I want to be able to use application.match for both .txtID and "Credit" under column 5
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How about this?

VBA Code:
Dim iRow1 As Long
  Dim iRow2 As Long
  Dim Sht As Worksheet
  
  Set Sht = Worksheets("Sales")
  With Me
    On Error Resume Next
    With Sht
      iRow1 = Application.Match(.txtID.Text, .Columns(1), 0)
      iRow2 = Application.Match(.txtID.Text, .Range(.Cells(iRow1 + 1, 1), .Cells(.Rows.Count, 1)), 0) + iRow1
      
    End With
  
    On Error GoTo 0
    
    If iRow1 > 0 Then
      If Sht.Cells(iRow1, 5).Value = "Cash" Then
        frmSales.txtitem.Text = Sht.Cells(iRow1, "B").Value
        .frmSales.txtquantity.Text = Sht.Cells(iRow1, "D").Value
        frmSales.txttotal.Text = Sht.Cells(iRow1, "C").Value
        
        frmSales.txtitem2.Text = Sht.Cells(iRow2, "B").Value
        .frmSales.txtquantity2.Text = Sht.Cells(iRow2, "D").Value
        frmSales.txttotal2.Text = Sht.Cells(iRow2, "C").Value
        
      ElseIf Sht.Cells(iRow2, 5).Value = "Cash" Then
        frmSales.txtitem.Text = Sht.Cells(iRow2, "B").Value
        .frmSales.txtquantity.Text = Sht.Cells(iRow2, "D").Value
        frmSales.txttotal.Text = Sht.Cells(iRow2, "C").Value
        
        frmSales.txtitem2.Text = Sht.Cells(iRow1, "B").Value
        .frmSales.txtquantity2.Text = Sht.Cells(iRow1, "D").Value
        frmSales.txttotal2.Text = Sht.Cells(iRow1, "C").Value
      End If
    End If
  End With
 
Upvote 0
How about this?

VBA Code:
Dim iRow1 As Long
  Dim iRow2 As Long
  Dim Sht As Worksheet
 
  Set Sht = Worksheets("Sales")
  With Me
    On Error Resume Next
    With Sht
      iRow1 = Application.Match(.txtID.Text, .Columns(1), 0)
      iRow2 = Application.Match(.txtID.Text, .Range(.Cells(iRow1 + 1, 1), .Cells(.Rows.Count, 1)), 0) + iRow1
     
    End With
 
    On Error GoTo 0
   
    If iRow1 > 0 Then
      If Sht.Cells(iRow1, 5).Value = "Cash" Then
        frmSales.txtitem.Text = Sht.Cells(iRow1, "B").Value
        .frmSales.txtquantity.Text = Sht.Cells(iRow1, "D").Value
        frmSales.txttotal.Text = Sht.Cells(iRow1, "C").Value
       
        frmSales.txtitem2.Text = Sht.Cells(iRow2, "B").Value
        .frmSales.txtquantity2.Text = Sht.Cells(iRow2, "D").Value
        frmSales.txttotal2.Text = Sht.Cells(iRow2, "C").Value
       
      ElseIf Sht.Cells(iRow2, 5).Value = "Cash" Then
        frmSales.txtitem.Text = Sht.Cells(iRow2, "B").Value
        .frmSales.txtquantity.Text = Sht.Cells(iRow2, "D").Value
        frmSales.txttotal.Text = Sht.Cells(iRow2, "C").Value
       
        frmSales.txtitem2.Text = Sht.Cells(iRow1, "B").Value
        .frmSales.txtquantity2.Text = Sht.Cells(iRow1, "D").Value
        frmSales.txttotal2.Text = Sht.Cells(iRow1, "C").Value
      End If
    End If
  End With
Thank you! That worked wonderfully, I appreciate it
 
Upvote 0

Forum statistics

Threads
1,215,726
Messages
6,126,503
Members
449,316
Latest member
sravya

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