LOOKUP type function with 2 different values

gareths

New Member
Joined
Aug 5, 2011
Messages
7
I have 2 sheets in my workbook on sheet1 there is a value for x in Q2 an a value for y in R2. In sheet 2 there are 3 columns - A contains x values, B contains Y values and C contains postcodes. What I would like to do is check the value of Q2 against those in column A on sheet2 and as soon as a value in A is higher than Q2 then R2 is checked against column B from that point until a value in B is higher than R2 and then the corresponding postcode in column C is the output.

For example: Q2 = 500877, R2 = 151590

on sheet2

_____A_______B________C
__141304___934035___HS20DB
__508783___100001___RH149RU
__508783___160389___TW178HL
__617755___168208___CT65JY

so here Q2 is less than 508783 in A and R2 is less than 160389 in B so the output is TW178HL.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I have 2 sheets in my workbook on sheet1 there is a value for x in Q2 an a value for y in R2. In sheet 2 there are 3 columns - A contains x values, B contains Y values and C contains postcodes. What I would like to do is check the value of Q2 against those in column A on sheet2 and as soon as a value in A is higher than Q2 then R2 is checked against column B from that point until a value in B is higher than R2 and then the corresponding postcode in column C is the output.

For example: Q2 = 500877, R2 = 151590

on sheet2

_____A_______B________C
__141304___934035___HS20DB
__508783___100001___RH149RU
__508783___160389___TW178HL
__617755___168208___CT65JY

so here Q2 is less than 508783 in A and R2 is less than 160389 in B so the output is TW178HL.
What if the X and Y values ARE EQUAL to the Q2 and R2 values? You specifically asked for GREATER so that's how I wrote the formula.

Book1
ABC
2141304934035HS20DB
3508783100001RH149RU
4508783160389TW178HL
5617755168208CT65JY
Sheet2

Book1
QRS
2500877151590TW178HL
Sheet1

This array formula** entered in S2:

=INDEX(Sheet2!C2:C5,MATCH(1,IF(Sheet2!A2:A5>Q2,IF(Sheet2!B2:B5>R2,1)),0))

** array formulas need to be entered using the key
combination of CTRL,SHIFT,ENTER (not just ENTER).
Hold down both the CTRL key and the SHIFT key
then hit ENTER.
 
Upvote 0
Yeah I mean greater than or equal to - I wasn't sure what statements you could use. I'll try that later thanks!
 
Upvote 0
Yeah that worked thanks, but the results I got back were not the ones required due to the lookup data not being correct therefore I am now using a web query to find the postcodes from an external site.

Code:
Private Sub commandbutton1_click()
    www = "URL;http://api1.nearby.org.uk/api/convert.php?key=431b753ede6408&p=" _
    & Range("U2") & "&want=postcode-uk&output=text"
    With ActiveSheet.QueryTables.Add(Connection:=www, Destination:=Range("W2"))

        .Name = "convert.php?key=431b753ede6408&p=""""&want=postcode-uk&output=text"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """postcodes"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

I'm just wondering if I'm able to change W2 so it is the active cell and U2 so it is the active cell - 2.
 
Upvote 0
Yeah that worked thanks, but the results I got back were not the ones required due to the lookup data not being correct therefore I am now using a web query to find the postcodes from an external site.

Code:
Private Sub commandbutton1_click()
    www = "URL;http://api1.nearby.org.uk/api/convert.php?key=431b753ede6408&p=" _
    & Range("U2") & "&want=postcode-uk&output=text"
    With ActiveSheet.QueryTables.Add(Connection:=www, Destination:=Range("W2"))
 
        .Name = "convert.php?key=431b753ede6408&p=""""&want=postcode-uk&output=text"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """postcodes"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

I'm just wondering if I'm able to change W2 so it is the active cell and U2 so it is the active cell - 2.
I'm not much of a programmer so I can't help you with that.

If, after a day or 2, no one else responds try starting a new thread.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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