Find row number based on on mulitple criteria

jmjeichavez

New Member
Joined
Sep 8, 2014
Messages
7
Hello,

I am looking for a VBA solution that can find the latest data entry using 2 criteria. For instances, looking at the table below, if i were to enter the employee ID and the product type it would return $79. I am trying to avoid formulas to accomplish this task. Any help would be much much appreciated!

EmployeeID
jc8277
Product
Gloves
Amount
$ 15.00
Updated
EmpID
Product
Amount
9/11/2017 13:08st4345Shoes $ 89.00
9/11/2017 13:11jc8277Gloves $ 15.00
9/11/2017 13:11jc8277Scarf $ 10.00
10/2/2017 12:54bb8899Shoes $ 90.00
10/2/2017 13:16st4345Gloves $ 15.00
10/2/2017 13:16st4345Shoes $ 100.00
10/24/2017 14:34jc8277Shoes $ 79.00
10/24/2017 14:34jc8277Gloves $ 15.00
11/3/2017 10:22bb8899Scarf $ 10.00
11/3/2017 10:22bb8899Gloves $ 15.00

<tbody>
</tbody>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
We need to know what columns these are in your image.

Are they column A to D?

And where exactly do you plan to enter the criterial for the search and where exactly will the result be entered.

And in your image
The same pair of gloves were shown in two different rows with different dates with the same emp number but same price so was not sure why you used this example.
 
Last edited:
Upvote 0
Yes they are column A to D..and i plan on using data validation to select the criteria employee ID in (B1) and Product in (B2) and have the amount auto populate in (B3). As for the example, even though the amount and the product is the same i need to have it select the latest amount based on date for both criteria even if its the same from a prior sale. Reason being, the amount can change depending on what the employees sales the gloves at.
 
Last edited:
Upvote 0
So since we have:

employee ID in (B1) and Product in (B2) and have the amount auto populate in (B3).

So we must start the search in row (4) I assume. Is that correct.
Or will we start in row 6
 
Last edited:
Upvote 0
This may be more difficult then I'm able to help with.
The problem is determining which date and time are the latest.

I can find employee number and product but then if there are many that meet that then I have to compare maybe many dates and times to see which one is latest.

Maybe someone else on this forum will have a answer.

I will try but hopefully someone else here will have a answer.
 
Upvote 0
How about
Code:
Sub findLatest()
   Dim LastSale As Date
   Dim Rng As Range
   Dim Cl As Range
   
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      Set Rng = .Range("A6", .Range("A" & Rows.Count).End(xlUp))
      
      With .Range("A5:D5")
         .AutoFilter 2, Range("B1").Value
         .AutoFilter 3, Range("B2").Value
      End With
      LastSale = WorksheetFunction.Subtotal(4, Rng)

      For Each Cl In Rng.SpecialCells(xlVisible)
         If Cl.Value = LastSale Then .Range("B3").Value = Cl.Offset(, 3).Value
      Next Cl
      .AutoFilterMode = False
   End With

End Sub
 
Upvote 0
Looks like that should work. I was attemting to compare dates and times against Now()
Your answer will work assuming all entry's are in listed in order by date.
Which it appears to be.

How about
Code:
Sub findLatest()
   Dim LastSale As Date
   Dim Rng As Range
   Dim Cl As Range
   
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      Set Rng = .Range("A6", .Range("A" & Rows.Count).End(xlUp))
      
      With .Range("A5:D5")
         .AutoFilter 2, Range("B1").Value
         .AutoFilter 3, Range("B2").Value
      End With
      LastSale = WorksheetFunction.Subtotal(4, Rng)

      For Each Cl In Rng.SpecialCells(xlVisible)
         If Cl.Value = LastSale Then .Range("B3").Value = Cl.Offset(, 3).Value
      Next Cl
      .AutoFilterMode = False
   End With

End Sub
 
Upvote 0
The order shouldn't matter as this line
Code:
LastSale = WorksheetFunction.Subtotal(4, Rng)
Should fine the highest number in the visible cells.
It's the same as the worksheet MAX function, but works on filtered data
 
Upvote 0
Another option, getting rid of the loop
Code:
Sub findLatest()
   Dim LastSale As Date
   Dim Rng As Range
   Dim Cl As Range
   
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      Set Rng = .Range("A6", .Range("A" & Rows.Count).End(xlUp))
      
      With .Range("A5:D5")
         .AutoFilter 2, Range("B1").Value
         .AutoFilter 3, Range("B2").Value
      End With
      LastSale = WorksheetFunction.Subtotal(4, Rng)
      Set Cl = Rng.Find(LastSale)
      If Not Cl Is Nothing Then
         Range("B3").Value = Cl.Offset(, 3).Value
      Else
         MsgBox "Nothing found"
      End If
      .AutoFilterMode = False
   End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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