How to use IFERROR, INDEX, MATCH if the amount of column is limited

gargilang

New Member
Joined
Jun 17, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hello, I would like to know if it would be possible to use the IFERROR, INDEX, MATCH function on below scenario.
Book1
ABCDEFGHIJ
1CountryCityImageCountryCity1City2City3Image1Image2Image3
2FranceParisParis_France_xxx.jpgFranceParis  Paris_France_xxx.jpg  
3CanadaTorontoToronto_Canada_xxx.jpgCanadaTorontoMontreal Toronto_Canada_xxx.jpgMontreal_Canada_yyy.jpg 
4CanadaMontrealMontreal_Canada_yyy.jpgUSAChicagoBostonMiamiChicago_USA_xxx.jpgBoston_USA_yyy.jpgMiami_USA_zzz.jpg
5USAChicagoChicago_USA_xxx.jpgUKLondonLiverpoolManchesterLondon_UK_xxx.jpgLiverpool_UK_yyy.jpgManchester_UK_zzz.jpg
6USABostonBoston_USA_yyy.jpgUKCambridgeCambridge_UK_aaaa.jpg
7USAMiamiMiami_USA_zzz.jpgJapanTokyoKyotoOsakaTokyo_Japan_xxx.jpgKyoto_Japan_yyy.jpgOsaka_Japan_zzz.jpg
8UKLondonLondon_UK_xxx.jpgJapanYokohamaNagoyaYokohama_Japan_aaaa.jpgNagoya_Japan_bbbb.jpg
9UKLiverpoolLiverpool_UK_yyy.jpg
10UKManchesterManchester_UK_zzz.jpg
11UKCambridgeCambridge_UK_aaaa.jpg
12JapanTokyoTokyo_Japan_xxx.jpg
13JapanKyotoKyoto_Japan_yyy.jpg
14JapanOsakaOsaka_Japan_zzz.jpg
15JapanYokohamaYokohama_Japan_aaaa.jpg
16JapanNagoyaNagoya_Japan_bbbb.jpg
Sheet2
Cell Formulas
RangeFormula
D2:D5D2=INDEX($A$2:$A$16, MATCH(0, COUNTIF($D$1:$D1, $A$2:$A$16), 0))
E2:G5,E7:G7E2=IFERROR(INDEX($B$2:$B$16, MATCH(0, COUNTIF($D2:D2,$B$2:$B$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
H2:H5,H7H2=IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:D2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
I2:J5,I7:J7I2=IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:H2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
D7D7=INDEX($A$2:$A$16, MATCH(0, COUNTIF($D$1:$D5, $A$2:$A$16), 0))
Press CTRL+SHIFT+ENTER to enter array formulas.

Based on the data of Countries and Cities filled in yellow on the left, by using the IFERROR, INDEX, MATCH formula I managed to get all the data I need. But since I limited the amount of column into 3, what if there are more than 3 City. What I want is for the excel to continue the list of cities by creating another row under it as example of row filled in red.

I hope it makes sence. Let me know if it's possible.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Formulas on their own do not "create another row". There is probably some obnoxious formula solution that would solve this but, and my bias is admittedly showing but I suggest, a VBA solution would be much more robust and simpler.
 
Upvote 0
Formulas on their own do not "create another row". There is probably some obnoxious formula solution that would solve this but, and my bias is admittedly showing but I suggest, a VBA solution would be much more robust and simpler.
hi Tom, thank you so much for replying. i am such a newbie with excel that even the formula that i use here i found online after 3 days of keep changing search keyword. If it not too much to ask. Could you show me the VBA solution that would achieve what I need?
 
Upvote 0
More info is needed to describe how the data in columns A, B, and C get there.

Are they fomulas, or manually entered, or imported from an external list, or copied and pasted from elsewhere in the workbook,
and
what is it that triggers the production of data in D:J.
and
is it updated manually or daily or weekly or every full moon or whatever.
 
Upvote 0
More info is needed to describe how the data in columns A, B, and C get there.

Are they fomulas, or manually entered, or imported from an external list, or copied and pasted from elsewhere in the workbook,
and
what is it that triggers the production of data in D:J.
and
is it updated manually or daily or weekly or every full moon or whatever.
Hi Tom, Thank you so much for your interest to answer. However I ended up asking a formula to make the Country data become unique within itself.
So based on the above table, for example Country UK will be UK1, UK1, UK1, UK2 and Country Japan will be Japan1, Japan1, Japan1, Japan2 and Japan2.
It should look like below:
Book2
ABCDEFGHIJ
1CountryCityImageCountryCity1City2City3Image1Image2Image3
2FranceParisParis_France_xxx.jpgFranceParis  Paris_France_xxx.jpg  
3CanadaTorontoToronto_Canada_xxx.jpgCanadaTorontoMontreal Toronto_Canada_xxx.jpgMontreal_Canada_yyy.jpg 
4CanadaMontrealMontreal_Canada_yyy.jpgUSAChicagoBostonMiamiChicago_USA_xxx.jpgBoston_USA_yyy.jpgMiami_USA_zzz.jpg
5USAChicagoChicago_USA_xxx.jpgUK1LondonLiverpoolManchesterLondon_UK_xxx.jpgLiverpool_UK_yyy.jpgManchester_UK_zzz.jpg
6USABostonBoston_USA_yyy.jpgUK2Cambridge  Cambridge_UK_aaaa.jpg  
7USAMiamiMiami_USA_zzz.jpgJapan1TokyoKyotoOsakaTokyo_Japan_xxx.jpgKyoto_Japan_yyy.jpgOsaka_Japan_zzz.jpg
8UK1LondonLondon_UK_xxx.jpgJapan2YokohamaNagoya Yokohama_Japan_aaaa.jpgNagoya_Japan_bbbb.jpg 
9UK1LiverpoolLiverpool_UK_yyy.jpg
10UK1ManchesterManchester_UK_zzz.jpg
11UK2CambridgeCambridge_UK_aaaa.jpg
12Japan1TokyoTokyo_Japan_xxx.jpg
13Japan1KyotoKyoto_Japan_yyy.jpg
14Japan1OsakaOsaka_Japan_zzz.jpg
15Japan2YokohamaYokohama_Japan_aaaa.jpg
16Japan2NagoyaNagoya_Japan_bbbb.jpg
Sheet1
Cell Formulas
RangeFormula
D2:D8D2=INDEX($A$2:$A$16, MATCH(0, COUNTIF($D$1:$D1, $A$2:$A$16), 0))
E2:G8E2=IFERROR(INDEX($B$2:$B$16, MATCH(0, COUNTIF($D2:D2,$B$2:$B$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
H2:H8H2=IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:D2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
I2:J8I2=IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:H2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
 
Upvote 0
Someone answered the same question I posted here. Please see below is the VBA code to perform above task I intended.
VBA Code:
Sub condense()
    Dim src, dest(), ws As Worksheet, srcRange As Range, i As Long, j As Long, countryCount As Long, rowNum As Long
    Set ws = ActiveSheet
    Set srcRange = ws.Cells(1, 1).Resize(ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, 3)
    src = srcRange.Value2
    ReDim dest(1 To UBound(src, 1) - 1, 1 To 7)
    rowNum = 1
    i = 2
    Do While i <= UBound(src, 1)
        countryCount = Application.CountIf(srcRange.Columns(1), src(i, 1))
        For j = 1 To countryCount
            dest(rowNum + Int((j - 1) / 3), 1) = src(i + j - 1, 1)
            dest(rowNum + Int((j - 1) / 3), 2 + ((j - 1) Mod 3)) = src(i + j - 1, 2)
            dest(rowNum + Int((j - 1) / 3), 5 + ((j - 1) Mod 3)) = src(i + j - 1, 3)
        Next j
        i = i + countryCount
        rowNum = rowNum + 1 + Int((countryCount - 1) / 3)
    Loop
    ws.Cells(2, 4).Resize(rowNum, 7).Value2 = dest
    With ws.Cells(1, 4).Resize(1, 7)
        .Value2 = Strings.Split("Country,City1,City2,City3,Image1,Image2,Image3", ",")
        .EntireColumn.AutoFit
    End With
End Sub
 
Upvote 0
Solution
Someone answered the same question I posted here.
I future please post any cross-post links at the time, not 8 days later.

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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