Use SWITCH() instead of nested IFS - possible?

dmj120

Active Member
Joined
Jan 5, 2010
Messages
310
Office Version
  1. 365
  2. 2019
  3. 2010
Can I use SWITCH() to streamline the below? This workbook is very large so I'm trying to keep efficiency at the top of the list.

Basically, I'm matching one cell to a range and need the header name of the corresponding match.

Excel Formula:
=IFERROR(IF([@[Priced as]]=[@[Like Mdl Pricing]], pricebook[[#Headers],[Like Mdl Pricing]],
   IF([@[Priced as]]=[@[AvgRL Price Device]],pricebook[[#Headers],[AvgRL Price Device]],
   IF([@[Priced as]]=[@[Avg RL Price Model]],pricebook[[#Headers],[Avg RL Price Model]],
   IF([@[Priced as]]=[@[Standard Price]],pricebook[[#Headers],[Standard Price]],
   IF([@[Priced as]]=[@[Avg Budget (Exp rpt)]],pricebook[[#Headers],[Avg Budget (Exp rpt)]],
   IF([@[Priced as]]=[@[RFP FSWP Avg.]],pricebook[[#Headers],[RFP FSWP Avg.]])))))),
"nada")
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about:


Excel Formula:
    SWITCH([@[Priced as]],
        [@[Like Mdl Pricing]], pricebook[[#Headers],[Like Mdl Pricing]],
        [@[AvgRL Price Device]], pricebook[[#Headers],[AvgRL Price Device]],
        [@[Avg RL Price Model]], pricebook[[#Headers],[Avg RL Price Model]],
        [@[Standard Price]], pricebook[[#Headers],[Standard Price]],
        [@[Avg Budget (Exp rpt)]], pricebook[[#Headers],[Avg Budget (Exp rpt)]],
        [@[RFP FSWP Avg.]], pricebook[[#Headers],[RFP FSWP Avg.]],
        "nada"
    )
 
Upvote 0
Solution
This workbook is very large so I'm trying to keep efficiency at the top of the list.
In that case you are better off with using your formula, rather than switch (or ifs) as they will evaluate everything, which a nested if does not do.
 
Upvote 0
Basically, I'm matching one cell to a range and need the header name of the corresponding match.

Is the range contiguous? In other words, do the columns appear next to each other, in the same order that you've listed, with nothing else in between?

If so, you could simply use INDEX-MATCH or XLOOKUP:

Excel Formula:
=IFERROR(INDEX(pricebook[[#Headers],[Like Mdl Pricing]:[RFP FSWP Avg.]], MATCH([@[Priced as]], pricebook[@[Like Mdl Pricing]:[RFP FSWP Avg.]], 0)), "nada")

Excel Formula:
=XLOOKUP([@[Priced as]], pricebook[@[Like Mdl Pricing]:[RFP FSWP Avg.]], pricebook[[#Headers],[Like Mdl Pricing]:[RFP FSWP Avg.]], "nada")

If not, you could also try something along these lines:

Excel Formula:
=XLOOKUP(
    [@[Priced as]],
    INDEX(([@[Like Mdl Pricing]],[@[AvgRL Price Device]],[@[Avg RL Price Model]],[@[Standard Price]],[@[Avg Budget (Exp rpt)]],[@[RFP FSWP Avg.]]),,,{1,2,3,4,5,6}),
    {"Like Mdl Pricing","AvgRL Price Device","Avg RL Price Model","Standard Price","Avg Budget (Exp rpt)","RFP FSWP Avg."},
    "nada"
)

...where the [area_num] argument of INDEX is used to convert the non-contiguous range reference into an array.

OR:

Excel Formula:
=XLOOKUP(
    [@[Priced as]],
    INDEX(pricebook[@],,{3,5,7,9,10,12}),
    {"Like Mdl Pricing","AvgRL Price Device","Avg RL Price Model","Standard Price","Avg Budget (Exp rpt)","RFP FSWP Avg."},
    "nada"
)

...where the [column_num] argument of INDEX is used to select the applicable columns from "This Row" (please note: {3,5,7,9,10,12} is just an example; adjust the column numbers to align with your actual dataset).
 
Upvote 0

Similar threads

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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