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!

[TABLE="width: 400"]
<tbody>[TR]
[TD]EmployeeID
[/TD]
[TD="align: right"]jc8277[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]Product
[/TD]
[TD="align: right"]Gloves[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]Amount
[/TD]
[TD="align: right"] $ 15.00[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]Updated
[/TD]
[TD="align: center"]EmpID
[/TD]
[TD="align: center"]Product
[/TD]
[TD="align: center"]Amount
[/TD]
[/TR]
[TR]
[TD="align: center"]9/11/2017 13:08[/TD]
[TD="align: center"]st4345[/TD]
[TD="align: center"]Shoes[/TD]
[TD="align: center"] $ 89.00[/TD]
[/TR]
[TR]
[TD="align: center"]9/11/2017 13:11[/TD]
[TD="align: center"]jc8277[/TD]
[TD="align: center"]Gloves[/TD]
[TD="align: center"] $ 15.00[/TD]
[/TR]
[TR]
[TD="align: center"]9/11/2017 13:11[/TD]
[TD="align: center"]jc8277[/TD]
[TD="align: center"]Scarf[/TD]
[TD="align: center"] $ 10.00[/TD]
[/TR]
[TR]
[TD="align: center"]10/2/2017 12:54[/TD]
[TD="align: center"]bb8899[/TD]
[TD="align: center"]Shoes[/TD]
[TD="align: center"] $ 90.00[/TD]
[/TR]
[TR]
[TD="align: center"]10/2/2017 13:16[/TD]
[TD="align: center"]st4345[/TD]
[TD="align: center"]Gloves[/TD]
[TD="align: center"] $ 15.00[/TD]
[/TR]
[TR]
[TD="align: center"]10/2/2017 13:16[/TD]
[TD="align: center"]st4345[/TD]
[TD="align: center"]Shoes[/TD]
[TD="align: center"] $ 100.00[/TD]
[/TR]
[TR]
[TD="align: center"]10/24/2017 14:34[/TD]
[TD="align: center"]jc8277[/TD]
[TD="align: center"]Shoes[/TD]
[TD="align: center"] $ 79.00[/TD]
[/TR]
[TR]
[TD="align: center"]10/24/2017 14:34[/TD]
[TD="align: center"]jc8277[/TD]
[TD="align: center"]Gloves[/TD]
[TD="align: center"] $ 15.00[/TD]
[/TR]
[TR]
[TD="align: center"]11/3/2017 10:22[/TD]
[TD="align: center"]bb8899[/TD]
[TD="align: center"]Scarf[/TD]
[TD="align: center"] $ 10.00[/TD]
[/TR]
[TR]
[TD="align: center"]11/3/2017 10:22[/TD]
[TD="align: center"]bb8899[/TD]
[TD="align: center"]Gloves[/TD]
[TD="align: center"] $ 15.00[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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