XLookup or Match Two things

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
I thought this would be simpler to figure out...but I can't quite get the result I'm after.

Mini-sheet attached.
Table has Population, Run (First, Second or Third) and a Sample Value.

Goal: User selects/enters population letter in G2, this returns in H2 the sample value from the table and in I2 the run in which the sample value occurred.

Example:
If A selected, then the sample value in column E is 15% and that occurs in the First Run.
If it had been B selected, then the sample would be 50% and that would be from the Third Run.

Note that despite having the same values of 31% in two runs in the table, in the larger dataset, the values are never actually the same when out to final decimal points.

I've tried a combination of INDEX/MATCH/OFFSET and XLOOKUP variations, but can't quite get it right for some reason.
Basically both things have to be matched up, the selected population and the sample for that selected population.

Book2
ABCDEFGHIJ
1PopulationFirstSecondThirdSampleSelectionSampleRun
2A15%31%31%15%A15%First
3B38%57%50%50%
4C41%15%53%15%Desired Result: If user selects A, get the sample value
5from column E, then match that to the
6corresponding run (Row1) and return that
Sheet1
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try:
VBA Code:
Sub MatchValues()
    Application.ScreenUpdating = False
    Dim fnd As Range, rng As Range, x As Double
    Set fnd = Range("A:A").Find(Range("G2").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        For Each rng In Range("B" & fnd.Row).Resize(, 3)
            x = Application.WorksheetFunction.Round(rng, 2)
            If x = Range("E" & fnd.Row) Then
                Cells(Rows.Count, "H").End(xlUp).Offset(1) = x
                Cells(Rows.Count, "I").End(xlUp).Offset(1) = Cells(1, rng.Column)
                Exit For
            End If
        Next rng
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub MatchValues()
    Application.ScreenUpdating = False
    Dim fnd As Range, rng As Range, x As Double
    Set fnd = Range("A:A").Find(Range("G2").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        For Each rng In Range("B" & fnd.Row).Resize(, 3)
            x = Application.WorksheetFunction.Round(rng, 2)
            If x = Range("E" & fnd.Row) Then
                Cells(Rows.Count, "H").End(xlUp).Offset(1) = x
                Cells(Rows.Count, "I").End(xlUp).Offset(1) = Cells(1, rng.Column)
                Exit For
            End If
        Next rng
    End If
    Application.ScreenUpdating = True
End Sub
This works nicely, thank you Mumps
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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