Not sure what formula to use.... Index match, Vlookup or ?

SelinaR

Board Regular
Joined
Feb 2, 2012
Messages
65
Office Version
  1. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
I have a mileage chart that runs on an x and a y-axis and need to have a drop-down that picks up a TO and FROM but in the correct axis.

I have tried multiple options but because the data has two prices (city to country and country to the city) I can't find a solution. o_O

I have made a drop-down box for from and to but as there are two sets of data, how can I build a formula to pick up the correct axis.?

Thanks so much in advance for your help (y)
 

Attachments

  • MIL Formula.JPG
    MIL Formula.JPG
    82.7 KB · Views: 10

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
what would the correct value for the example in the picture, Albany to Bunbury? 300, 350, 650?
 
Upvote 0
what would the correct value for the example in the picture, Albany to Bunbury? 300, 350, 650?
Albany to Bunbury = 350 (down and across to the right)
AND
Bunbury to Albany = 300 (up and across (to the left)


A to B & B to A.JPG
 
Upvote 0
Here are a couple of options.

VBA
20200910 MXL Intersect.xlsm
BCDEFGHIJKLM
2FromALBANY300
3ToBUNBURY350
4PERTH APO855605801991705961155039368245
50PERTH CITY25495249111665150714794583763
625943FREEMANTLE58067989963693636958728624
725153677OSBORNE PARK15146197840474950332297
825302966814WELSHPOOL717339633530346575360
9259812059645WANGARA636670707194472333
1025367701422944118REDCLIFF8921734108340
11300398974491914970292MIDLAND124834438399
121000152357395608825285217ALBANY119300839
1390367812951313376177851601BROOME358110
1475099679588814579672344350307BUNBURY804
15450783338355444428810557380560CARNARVON
Sheet1


VBA Code:
'Worksheet Code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C2:C3"), Target) Is Nothing Then Dist
End Sub

'Standard Module
Sub Dist()
Dim r As Range:         Set r = Range("B4:M" & Range("B" & Rows.Count).End(xlUp).Row)
Dim iFrom As String:    iFrom = Range("C2").Value
Dim iTo As String:      iTo = Range("C3").Value
Dim fCel As Range:      Set fCel = r.Find(iFrom, r.Cells(1), xlValues, xlWhole)
Dim sCel As Range:      Set sCel = r.Find(iTo, r.Cells(1), xlValues, xlWhole)

Range("D2").Value = Application.Intersect(Range(Rows(fCel.Row).Address), Range(Columns(sCel.Column).Address))
Range("D3").Value = Application.Intersect(Range(Rows(sCel.Row).Address), Range(Columns(fCel.Column).Address))
End Sub

Power Query
20200910 MXL Intersect.xlsm
OP
2CityDist
3REDCLIFF340
4CARNARVON88
Sheet1


Power Query:
(city1 as text, city2 as text) as table =>

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Unpivot = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"),
    GroupCol = Table.AddColumn(Unpivot, "Custom", each if Value.Is([Value], type text) then [Value] else null),
    FilledDown = Table.FillDown(GroupCol,{"Custom"}),
    ColumnCol = Table.AddColumn(FilledDown, "Column", each if [Value]=[Custom] then [Attribute] else null),
    Group = Table.Group(ColumnCol, {"Custom"}, {{"Count", each _, type table}}),
    cID = Table.AddIndexColumn(Group, "cID", 0, 1),
    RC = Table.RemoveColumns(cID,{"cID"}),
    iFrom = RC{cID{[Custom=city1]}[cID]}[Count][Column]{0},
    iTo = RC{cID{[Custom=city2]}[cID]}[Count][Column]{0},
    c1 = cID{[Custom=city1]}[cID],
    c2 = cID{[Custom=city2]}[cID]-1,
    Result = Table.FromRecords({[City=RC{c1}[Count]{[Attribute=iTo]}[Custom], Dist=RC{c1}[Count]{[Attribute=iTo]}[Value]],[City=RC{c2+1}[Count]{0}[Value],Dist=RC{c2}[Count]{[Attribute=iFrom]}[Value]]})
in
    Result

The Power query code is a custom function that you invoke by entering in the 2 city names that you want to look up.
 
Upvote 0
Here's a formula solution as well.

20200910 MXL Intersect.xlsm
BCDEFGHIJKLM
2FromWELSHPOOL346346
3ToBROOME313313
4PERTH APO855605801991705961155039368245
50PERTH CITY25495249111665150714794583763
625943FREEMANTLE58067989963693636958728624
725153677OSBORNE PARK15146197840474950332297
825302966814WELSHPOOL717339633530346575360
9259812059645WANGARA636670707194472333
1025367701422944118REDCLIFF8921734108340
11300398974491914970292MIDLAND124834438399
121000152357395608825285217ALBANY119300839
1390367812951313376177851601BROOME358110
1475099679588814579672344350307BUNBURY804
15450783338355444428810557380560CARNARVON
Sheet1
Cell Formulas
RangeFormula
E2E2=INDEX(City,SUMPRODUCT(($B$4:$M$15=C2)*ROW($B$4:$M$15))-3,SUMPRODUCT(($B$4:$M$15=C3)*ROW($B$4:$M$15))-3)
E3E3=INDEX(City,SUMPRODUCT(($B$4:$M$15=C3)*ROW($B$4:$M$15))-3,SUMPRODUCT(($B$4:$M$15=C2)*ROW($B$4:$M$15))-3)
 
Upvote 0
If you are happy to change the data slightly, you could use this simpler formula
+Fluff New.xlsm
ABCDEFGHIJKLMN
1
2FromAlbany350
3ToBunbury
4PERTH APOPERTH CITYFREEMANTLEOSBORNE PARKWELSHPOOLWANGARAREDCLIFFMIDLANDALBANYBROOMEBUNBURYCARNARVON
5PERTH APO855605801991705961155039368245
6PERTH CITY025495249111665150714794583763
7FREEMANTLE2594358067989963693636958728624
8OSBORNE PARK2515367715146197840474950332297
9WELSHPOOL25302966814717339633530346575360
10WANGARA259812059645636670707194472333
11REDCLIFF253677014229441188921734108340
12MIDLAND300398974491914970292124834438399
13ALBANY1000152357395608825285217119300839
14BROOME90367812951313376177851601358110
15BUNBURY75099679588814579672344350307804
16CARNARVON450783338355444428810557380560
Master
Cell Formulas
RangeFormula
D2D2=INDEX(C5:N16,MATCH(C3,B5:B16,0),MATCH(C2,C4:N4,0))
 
Upvote 0
In my previous solution I failed to mention that I used a named range for B4:M15. I also didn't replace all the range references.

20200910 MXL Intersect.xlsm
BCDEFGHIJKLM
2FromWELSHPOOL339339
3ToREDCLIFF944944
4PERTH APO855605801991705961155039368245
50PERTH CITY25495249111665150714794583763
625943FREEMANTLE58067989963693636958728624
725153677OSBORNE PARK15146197840474950332297
825302966814WELSHPOOL717339633530346575360
9259812059645WANGARA636670707194472333
1025367701422944118REDCLIFF8921734108340
11300398974491914970292MIDLAND124834438399
121000152357395608825285217ALBANY119300839
1390367812951313376177851601BROOME358110
1475099679588814579672344350307BUNBURY804
15450783338355444428810557380560CARNARVON
Sheet1
Cell Formulas
RangeFormula
E2E2=INDEX(City,SUMPRODUCT((City=C2)*ROW(City))-3,SUMPRODUCT((City=C3)*ROW(City))-3)
E3E3=INDEX(City,SUMPRODUCT((City=C3)*ROW(City))-3,SUMPRODUCT((City=C2)*ROW(City))-3)
 
Upvote 0
Hiya - thanks for this I will try the various option provided :)
 
Upvote 0
If you are happy to change the data slightly, you could use this simpler formula
+Fluff New.xlsm
ABCDEFGHIJKLMN
1
2FromAlbany350
3ToBunbury
4PERTH APOPERTH CITYFREEMANTLEOSBORNE PARKWELSHPOOLWANGARAREDCLIFFMIDLANDALBANYBROOMEBUNBURYCARNARVON
5PERTH APO855605801991705961155039368245
6PERTH CITY025495249111665150714794583763
7FREEMANTLE2594358067989963693636958728624
8OSBORNE PARK2515367715146197840474950332297
9WELSHPOOL25302966814717339633530346575360
10WANGARA259812059645636670707194472333
11REDCLIFF253677014229441188921734108340
12MIDLAND300398974491914970292124834438399
13ALBANY1000152357395608825285217119300839
14BROOME90367812951313376177851601358110
15BUNBURY75099679588814579672344350307804
16CARNARVON450783338355444428810557380560
Master
Cell Formulas
RangeFormula
D2D2=INDEX(C5:N16,MATCH(C3,B5:B16,0),MATCH(C2,C4:N4,0))
Hiya - thanks for this one... I previously tried to use this option, but as the $ are different from and to and then to and from it wasn't accurate
 
Upvote 0
In my previous solution I failed to mention that I used a named range for B4:M15. I also didn't replace all the range references.

20200910 MXL Intersect.xlsm
BCDEFGHIJKLM
2FromWELSHPOOL339339
3ToREDCLIFF944944
4PERTH APO855605801991705961155039368245
50PERTH CITY25495249111665150714794583763
625943FREEMANTLE58067989963693636958728624
725153677OSBORNE PARK15146197840474950332297
825302966814WELSHPOOL717339633530346575360
9259812059645WANGARA636670707194472333
1025367701422944118REDCLIFF8921734108340
11300398974491914970292MIDLAND124834438399
121000152357395608825285217ALBANY119300839
1390367812951313376177851601BROOME358110
1475099679588814579672344350307BUNBURY804
15450783338355444428810557380560CARNARVON
Sheet1
Cell Formulas
RangeFormula
E2E2=INDEX(City,SUMPRODUCT((City=C2)*ROW(City))-3,SUMPRODUCT((City=C3)*ROW(City))-3)
E3E3=INDEX(City,SUMPRODUCT((City=C3)*ROW(City))-3,SUMPRODUCT((City=C2)*ROW(City))-3)
did you name the C2 and C3 cells as "city" ???
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,031
Members
449,092
Latest member
ikke

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