Reputation: 5779
I'm trying to do the following:
N/A
or No
; if it has either of these then it should output N/A
or No
£
or €
or Yes
; If it has one of these then it would continue to step 3. If it has $
then it should repeat the same input as the output.REGEXEXTRACT(A1, "\$\d+")
or REGEXEXTRACT(A1, "\£\d+")
(I assume that's the best way)GoogleFinance("CURRENCY:EURUSD")
or GoogleFinance("CURRENCY:GBPUSD")
N/A
--> N/A
No
--> No
Alt
--> Alt
Yes
--> Yes
Yes £10
--> Yes $12.19
Yes £10 per week
--> Yes $12.19 per week
Yes €5 (Next)
--> Yes $5.49 (Next)
Yes $5 22 EA
--> Yes $5 22 EA
Yes £5 - £10
--> Yes $5.49 - $12.19
I am unable to get a working IF
statement working, I could do this in normal code but can't work it out for spreadsheet formulas.
I've tried modifying @Rubén's answer lots of times to including the N/A
as it's not the Sheets error, I also tried the same for making any USD inputs come out as USD (no changes) but I really can't get the hang of IF/OR/AND in Excel/Google Sheets.
=ArrayFormula(
SUBSTITUTE(
A1,
OR(IF(A1="No","No",REGEXEXTRACT(A1, "[\£|\€]\d+")),IF(A1="N/A","N/A",REGEXEXTRACT(A1, "[\£|\€]\d+"))),
IF(
A1="No",
"No",
TEXT(
REGEXEXTRACT(A1, "[\£|\€](\d+)")*
IF(
"€"=REGEXEXTRACT(A1, "([\£|\€])\d+"),
GoogleFinance("CURRENCY:EURUSD"),
GoogleFinance("CURRENCY:GBPUSD")
),
"$###,###"
)
)
)
)
The above, I tried to add an OR() before the first IF statement to try and include N/A
as an option, in the below I tried it as you can see below in various different ways (replace line 4 with this)
IF(
OR(
A1="No",
"No",
REGEXEXTRACT(A1, "[\£|\€]\d+");
A1="No",
"No",
REGEXEXTRACT(A1, "[\£|\€]\d+")
)
)
But that doesn't work either. I thought using ;
was a way to separate the OR expressions but apparently not.
I've modified it to =ArrayFormula( IF(NOT(ISBLANK(A2)),
IF(IFERROR(SEARCH("$",A2),0),A2,IF(A2="N/A","N/A",IF(A2="No","No",IF(A2="Alt","Alt",IF(A2="Yes","Yes",
SUBSTITUTE(
A2,
REGEXEXTRACT(A2, "[\£|\€]\d+"),
TEXT(
REGEXEXTRACT(A2, "[\£|\€](\d+)")
*
VLOOKUP(
REGEXEXTRACT(A2, "([\£|\€])\d+"),
{
{"£";"€"},
{GoogleFinance("CURRENCY:GBPUSD");GoogleFinance("CURRENCY:EURUSD")}
},
2,0),
"$###,###"
)
)
)))))
,"")
)
This fixes:
Yes
only cells no longer throw #N/AAlt
As you can see in the image below the two red cells aren't quite correct as I never thought of this scenario, the second of the two values is staying in it's input form and not being converted to USD.
Upvotes: 1
Views: 2387
Reputation: 38286
Try
=ArrayFormula( IF(IFERROR(SEARCH("$",A1:A6),0),A1:A6,IF(A1:A6="N/A","N/A",IF(A1:A6="No","No", SUBSTITUTE( A1:A6, REGEXEXTRACT(A1:A6, "[\£|\€]\d+"), TEXT( REGEXEXTRACT(A1:A6, "[\£|\€](\d+)") * VLOOKUP( REGEXEXTRACT(A1:A6, "([\£|\€])\d+"), { {"£";"€"}, {GoogleFinance("CURRENCY:GBPUSD");GoogleFinance("CURRENCY:EURUSD")} }, 2,0), "$###,###.00" ) ) ))) )
Result
+---+------------------+---------------------+ | | A | B | +---+------------------+---------------------+ | 1 | N/A | N/A | | 2 | No | No | | 3 | Yes £10 | Yes $12.19 | | 4 | Yes £10 per week | Yes $12.19 per week | | 5 | Yes €5 (Next) | Yes $5.49 (Next) | +---+------------------+---------------------+
Instead or using OR function, the above formula use nested IF functions.
Instead of using a REGEXEXTRACT function for each currency symbol, a regex OR operator was used. Example
REGEXEXTRACT(A1:A6, "[\£|\€]\d+")
Three regular expressions were used,
[\£|\€]\d+
[\£|\€](\d+)
[(\£|\€])\d+
Instead of using nested IF to handle currency conversion rates, VLOOKUP and array is used. This could be make easier to maintain the formula assuming that more currencies could be added in the future.
Upvotes: 2