Excel strip the bracket and display its value - excel

I am new to excel, I need to remove the M from the string and if the value is in bracket, set it as negative, else set it as positive. But I only managed to do it for positive, how can I write a function that fulfills the mentioned condition?
If G1 value is (100M) then show -100
If G1 value is 100M then show 100
=LEFT(G1,LEN(G1)-1)

=VALUE(IF(LEFT(G1,1)="(",SUBSTITUTE(LEFT(G1,LEN(G1)-2),"(","-"),LEFT(G1,LEN(G1)-1)))

I'm sure there are quite a few ways to doing this but there are the basic steps.
First you need to check your string for the letter M with =search("What",Where). This returns the location of the character if it exists and #VALUE if not.
Next we need to evaluate if the M was found in our search, this can be done with =IFERROR(value) where it will return true if the character is not found.
Lastly we need to wrap it all in a =IF(value, true, false) statement. I'm using =VALUE(string) to convert the value from a string to a number
=IF(IFERROR(SEARCH("M",A2)),-VALUE(LEFT(A2,LEN(A2)-1)),VALUE(A2))
The drawback with an approach is it does not check that M is the last letter in the string, so it would fail with something like 100M100, this could be easily tested for and corrected by checking for the Position of M with =SEARCH() and only returning =SEARCH()-1 characters with =LEFT(A1,SEARCH()-1)
Like:
=IF(IFERROR(SEARCH("M",A1)),-VALUE(LEFT(A1,SEARCH("M",A1)-1)),VALUE(A1))
If A1 was 100M100 then it would return -100
UPDATE:
I originally misread the question missing the parenthesis. both Abe Gold and Tom Sharpe had correctly done this so I will update my fix my mistake. In this case, assumed that and M was significant to the sign of the result and the parenthesis were redundant. So to correct my answer I would do the following
=if(IFERROR(search(")",G1)),-VALUE(right(left(G1,search(")",G1)-2),SEARCH(")",G1)-3)),value(LEFT(G1, LEN(G1)-1)))

=IF(LEFT(G1)="(",-MID(G1,2,LEN(G1)-3),--LEFT(G1,LEN(G1)-1))

This is nicer i think
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(G1,"M",""),")",""),"(","-"))

Not sure of all the conditions, but for the following
G1 might be numbers with or without the M and the (...)
You always want to return the number if present.
If that is the case, then simply:
=VALUE(SUBSTITUTE(G1,"M",""))
You have not indicated what you would want for a return should the value in G1 be non-numeric. Just wrap the above in an IFERROR function to allow for that.

Related

IF ISNUMBER SEARCH - distinguish between similar text

Hoping someone smarter than myself can help:
Column F contains either of the words below, and I want to excel formula to return whether the data is "White" or "Pink"
Ban
Bandearg
=IF(ISNUMBER(SEARCH("Ban",F2)),"White",IF(ISNUMBER(SEARCH("Bandearg",F2)),"Pink")
The problem is that everything returns as "White" as it is finding "Ban" in both. Presume I am using the formula wrong.
Bán is the Irish for White and Bándearg is the Irish for Pink!
As per my comment, simply swap them around:
=IF(ISNUMBER(SEARCH("Bandearg",F2)),"Pink",IF(ISNUMBER(SEARCH("Ban",F2)),"White"))
ISNUMBER checks wheather or not SEARCH returns a number (meaning the substring is found starting at that index/position) or an #VALUE error. In the first case, it will return TRUE, otherwise FALSE continueing with the second nested IF. Note that this formula will return FALSE if "Ban" isn't found at all.
Maybe just simply add a space behind the criteria and the result value, and your formula become :
=IF(ISNUMBER(SEARCH("Ban ",F2&" ")),"White",IF(ISNUMBER(SEARCH("Bandearg ",F2&" ")),"Pink"))
If those words are all that the cell contains, you can simply use:
=IF(A1="Ban","White",IF(A1="Bandearg","Pink",""))
or, in later versions of Excel (2019+ or O365):
=IFS(A1="Ban","White",A1="Bandearg","Pink",TRUE,"")

Excel matching IF statement not evaluating correctly

I have two columns in the following structure
A B
1 49 4922039670
I have been able to evaluate =LEN(A1) as 2, =LEFT(B1,2) as 49, and =LEFT(B1,LEN(A1)) as 49. However when I try to evaluate =IF(LEFT(B1,LEN(A1))=A1,TRUE,FALSE) it evaluates FALSE. I have no idea why. I have tried to change both columns A and B to different matching formats but it still just evaluates FALSE.
Not entirely sure what Im doing wrong.
maybe it is depending on the Version but in my Excel it you cast implicit your number 4922... to a string and 49 <> '49'
Either you make both strings or both numbers, f.e.:
=IF(LEFT(B1,LEN(A1))=""&A1,TRUE,FALSE)
""&<a number> concates an empty string to a number, the result will be a string
you could use function "TEXT" or - if you are sure, both are numbers - use 'VALUE'
=IF(VALUE(LEFT(B1,LEN(A1)))=A1,TRUE,FALSE)
to make a text should be more stable
Comparing a string (what LEFT gives you) with an integer is going to be FALSE
=LEFT(B1,LEN(A1))=49 //FALSE
You want
=LEFT(B1,LEN(A1))="49" //TRUE
So:
=LEFT(B1,LEN(A1))=TEXT(A1, "0")
Or:
=EXACT(LEFT(B1,LEN(A1)),A1)

VLOOKUP not finding value in array

I used VLOOKUP function to find a value in an array but some of the values gave #N/A answer despite of available in array.
To round up the numbers, I used CEILING function but interesting point is in some values, it did not work.
I checked the type of value if it is number or not.
Also, I used ROUNDUP function but did not work.
Also, I tried INDEX/MATCH combination and again did not work.
In the example that I gave in the link, when I type between 15.00 - 15.20, it gives error but trying other values, it works.
How do I fix this?
This seems to be a bug with VLOOKUP and MATCH using the return values of CEILING. If you use:
=VLOOKUP(ROUND(CEILING(F4,0.1),1),A:B,2,FALSE)
then it works as expected.
If we look at this with VBA then we see what happens. To blame should be really CEILING and ROUNDUP. See example:
Sub testCeilingAndRoundup()
Dim v As Double, test As Boolean, diff As Double
v = [CEILING(15.1,0.1)] '15.1
test = (v = 15.1) 'FALSE
diff = 15.1 - v '-1.776...E-15
v = [ROUNDUP(15.25,1)] '15.3
test = (v = 15.3) 'FALSE
diff = 15.3 - v '1.776...E-15
End Sub
Looks like you've run into an Excel bug.
Applying CEILING to the number 15.1 should return the exact same result (15.1) regardless of whether the significance is 0.1, 0.01, 0.001, etc.
And indeed it does, according to Excel: when asked if they're equal, the answer is always TRUE.
But looking up these mathematically equal numbers in the look-up table gives different results.
This has to be a bug.
Instead of CEILING(F4,0.1), I suggest you use ROUNDUP(F4,1) which appears to be bug-free. Nope, ROUNDUP is buggy as well. Axel Richter's answer suggests wrapping CEILING in a ROUND and this does appear to make the problem go away. You can also convert to string and back to number:
VALUE(TEXT(ROUNDUP(F4,1),"0.0"))
so you'd have
=VLOOKUP(VALUE(TEXT(ROUNDUP(F4,1),"0.0")),A:B,2,FALSE)
Your CEILING function should be more precise if you want to find a match for 15.10
Change it to CEILING(F4,0.01) and it will works :)

Excel copying part of a string between characters

Is there a way of returning part of a string between certain characters in excel? For example my string looks like this:
`switchrefid` = {switchrefid: }
I need to cut the part of the string between the ' (apostrophes) so it just returns switchrefid
I'm sure there must be a formula for this i just cant think of the one to use.
Thanks in advance.
As long as the ``` characters occur exactly twice in your data, you can do:
=LEFT(RIGHT(A1, LEN(A1)-FIND("`", A1)), FIND("`",RIGHT(A1, LEN(A1)-FIND("`", A1)))-1)
Although it is pretty horrible!
(Edit: this assumes your data is in A1, of course.)
Just two more options, if the word always starts at the second Character and ends just before the last you could simply use :
=MID(A1,2,LEN(A1)-2) ' Minus 2 for the 2 ticks
And the second option would be to substitute the tick with nothing like so:
=SUBSTITUTE(A1,"`","")
With the substitute is also supports a number of substitutes. So if you had `switchrefid`` for some reason and only want to get rid of 2 of the three ticks you could use:
=SUBSTITUTE(A1,"`","",2)
and this would return switchrefid`
although it would not work for ''switchrefid' as it would STILL return switchrefid' because it only removes the 1st 2 instances of the text to remove

Case Function Equivalent in Excel

I have an interesting challenge - I need to run a check on the following data in Excel:
| A - B - C - D |
|------|------|------|------|
| 36 | 0 | 0 | x |
| 0 | 600 | 700 | x |
|___________________________|
You'll have to excuse my wonderfully bad ASCII art. So I need the D column (x) to run a check against the adjacent cells, then convert the values if necessary. Here's the criteria:
If column B is greater than 0, everything works great and I can get coffee. If it doesn't meet that requirement, then I need to convert A1 according to a table - for example, 32 = 1420 and place into D. Unfortunately, there is no relationship between A and what it needs to convert to, so creating a calculation is out of the question.
A case or switch statement would be perfect in this scenario, but I don't think it is a native function in Excel. I also think it would be kind of crazy to chain a bunch of =IF() statements together, which I did about four times before deciding it was a bad idea (story of my life).
Sounds like a job for VLOOKUP!
You can put your 32 -> 1420 type mappings in a couple of columns somewhere, then use the VLOOKUP function to perform the lookup.
Without reference to the original problem (which I suspect is long since solved), I very recently discovered a neat trick that makes the Choose function work exactly like a select case statement without any need to modify data. There's only one catch: only one of your choose conditions can be true at any one time.
The syntax is as follows:
CHOOSE(
(1 * (CONDITION_1)) + (2 * (CONDITION_2)) + ... + (N * (CONDITION_N)),
RESULT_1, RESULT_2, ... , RESULT_N
)
On the assumption that only one of the conditions 1 to N will be true, everything else is 0, meaning the numeric value will correspond to the appropriate result.
If you are not 100% certain that all conditions are mutually exclusive, you might prefer something like:
CHOOSE(
(1 * TEST1) + (2 * TEST2) + (4 * TEST3) + (8 * TEST4) ... (2^N * TESTN)
OUT1, OUT2, , OUT3, , , , OUT4 , , <LOTS OF COMMAS> , OUT5
)
That said, if Excel has an upper limit on the number of arguments a function can take, you'd hit it pretty quickly.
Honestly, can't believe it's taken me years to work it out, but I haven't seen it before, so figured I'd leave it here to help others.
EDIT: Per comment below from #aTrusty:
Silly numbers of commas can be eliminated (and as a result, the choose statement would work for up to 254 cases) by using a formula of the following form:
CHOOSE(
1 + LOG(1 + (2*TEST1) + (4*TEST2) + (8*TEST3) + (16*TEST4),2),
OTHERWISE, RESULT1, RESULT2, RESULT3, RESULT4
)
Note the second argument to the LOG clause, which puts it in base 2 and makes the whole thing work.
Edit: Per David's answer, there's now an actual switch statement if you're lucky enough to be working on office 2016. Aside from difficulty in reading, this also means you get the efficiency of switch, not just the behaviour!
The Switch function is now available, in Excel 2016 / Office 365
SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
example:
=SWITCH(A1,0,"FALSE",-1,"TRUE","Maybe")
Microsoft -Office Support
Note: MS has updated that page to only document the behavior of Excel 2019. Eventually, they will probably remove references to 2019 as well... To see what the page looked like in 2016, use the wayback machine:
https://web.archive.org/web/20161010180642/https://support.office.com/en-us/article/SWITCH-function-47ab33c0-28ce-4530-8a45-d532ec4aa25e
Try this;
=IF(B1>=0, B1, OFFSET($X$1, MATCH(B1, $X:$X, Z) - 1, Y)
WHERE
X = The columns you are indexing into
Y = The number of columns to the left (-Y) or right (Y) of the indexed column to get the value you are looking for
Z = 0 if exact-match (if you want to handle errors)
I used this solution to convert single letter color codes into their descriptions:
=CHOOSE(FIND(H5,"GYR"),"Good","OK","Bad")
You basically look up the element you're trying to decode in the array, then use CHOOSE() to pick the associated item. It's a little more compact than building a table for VLOOKUP().
I know it a little late to answer but I think this short video will help you a lot.
http://www.xlninja.com/2012/07/25/excel-choose-function-explained/
Essentially it is using the choose function. He explains it very well in the video so I'll let do it instead of typing 20 pages.
Another video of his explains how to use data validation to populate a drop down which you can select from a limited range.
http://www.xlninja.com/2012/08/13/excel-data-validation-using-dependent-lists/
You could combine the two and use the value in the drop down as your index to the choose function. While he did not show how to combine them, I'm sure you could figure it out as his videos are good. If you have trouble, let me know and I'll update my answer to show you.
I understand that this is a response to an old post-
I like the If() function combined with Index()/Match():
=IF(B2>0,"x",INDEX($H$2:$I$9,MATCH(A2,$H$2:$H$9,0),2))
The if function compare what is in column b and if it is greater than 0, it returns x, if not it uses the array (table of information) identified by the Index() function and selected by Match() to return the value that a corresponds to.
The Index array has the absolute location set $H$2:$I$9 (the dollar signs) so that the place it points to will not change as the formula is copied. The row with the value that you want returned is identified by the Match() function. Match() has the added value of not needing a sorted list to look through that Vlookup() requires. Match() can find the value with a value: 1 less than, 0 exact, -1 greater than. I put a zero in after the absolute Match() array $H$2:$H$9 to find the exact match. For the column that value of the Index() array that one would like returned is entered. I entered a 2 because in my array the return value was in the second column. Below my index array looked like this:
32 1420
36 1650
40 1790
44 1860
55 2010
The value in your 'a' column to search for in the list is in the first column in my example and the corresponding value that is to be return is to the right. The look up/reference table can be on any tab in the work book - or even in another file. -Book2 is the file name, and Sheet2 is the 'other tab' name.
=IF(B2>0,"x",INDEX([Book2]Sheet2!$A$1:$B$8,MATCH(A2,[Book2]Sheet2!$A$1:$A$8,0),2))
If you do not want x return when the value of b is greater than zero delete the x for a 'blank'/null equivalent or maybe put a 0 - not sure what you would want there.
Below is beginning of the function with the x deleted.
=IF(B2>0,"",INDEX...
If you don't have a SWITCH statement in your Excel version (pre-Excel-2016), here's a VBA implementation for it:
Public Function SWITCH(ParamArray args() As Variant) As Variant
Dim i As Integer
Dim val As Variant
Dim tmp As Variant
If ((UBound(args) - LBound(args)) = 0) Or (((UBound(args) - LBound(args)) Mod 2 = 0)) Then
Error 450 'Invalid arguments
Else
val = args(LBound(args))
i = LBound(args) + 1
tmp = args(UBound(args))
While (i < UBound(args))
If val = args(i) Then
tmp = args(i + 1)
End If
i = i + 2
Wend
End If
SWITCH = tmp
End Function
It works exactly like expected, a drop-in replacement for example for Google Spreadsheet's SWITCH function.
Syntax:
=SWITCH(selector; [keyN; valueN;] ... defaultvalue)
where
selector is any expression that is compared to keys
key1, key2, ... are expressions that are compared to the selector
value1, value2, ... are values that are selected if the selector equals to the corresponding key (only)
defaultvalue is used if no key matches the selector
Examples:
=SWITCH("a";"?") returns "?"
=SWITCH("a";"a";"1";"?") returns "1"
=SWITCH("x";"a";"1";"?") returns "?"
=SWITCH("b";"a";"1";"b";TRUE;"?") returns TRUE
=SWITCH(7;7;1;7;2;0) returns 2
=SWITCH("a";"a";"1") returns #VALUE!
To use it, open your Excel, go to Develpment tools tab, click Visual Basic, rightclick on ThisWorkbook, choose Insert, then Module, finally copy the code into the editor. You have to save as a macro-friendly Excel workbook (xlsm).
Even if old, this seems to be a popular questions, so I'll post another solution, which I think is very elegant:
http://fiveminutelessons.com/learn-microsoft-excel/using-multiple-if-statements-excel
It's elegant because it uses just the IF function. Basically, it boils down to this:
if(condition, choose/use a value from the table, if(condition, choose/use another value from the table...
And so on
Works beautifully, even better than HLOOKUP or VLOOOKUP
but... Be warned - there is a limit to the number of nested if statements excel can handle.
Microsoft replace SWITCH, IFS and IFVALUES with CHOOSE only function.
=CHOOSE($L$1,"index_1","Index_2","Index_3")
Recently I unfortunately had to work with Excel 2010 again for a while and I missed the SWITCH function a lot. I came up with the following to try to minimize my pain:
=CHOOSE(SUM((A1={"a";"b";"c"})*ROW(INDIRECT(1&":"&3))),1,2,3)
CTRL+SHIFT+ENTER
where A1 is where your condition lies (it could be a formula, whatever). The good thing is that we just have to provide the condition once (just like SWITCH) and the cases (in this example: a,b,c) and results (in this example: 1,2,3) are ordered, which makes it easy to reason about.
Here is how it works:
Cond={"c1";"c2";...;"cn"} returns a N-vector of TRUE or FALSE (with behaves like 1s and 0s)
ROW(INDIRECT(1&":"&n)) returns a N-vector of ordered numbers: 1;2;3;...;n
The multiplication of both vectors will return lots of zeros and a number (position) where the condition was matched
SUM just transforms this vector with zeros and a position into just a single number, which CHOOSE then can use
If you want to add another condition, just remember to increment the last number inside INDIRECT
If you want an ELSE case, just wrap it inside an IFERROR formula
The formula will not behave properly if you provide the same condition more than once, but I guess nobody would want to do that anyway
If your using Office 2016 or later, or Office 365, there is a new function that acts similarly to a CASE function called IFS. Here's the description of the function from Microsoft's documentation:
The IFS function checks whether one or more conditions are met, and returns a value that corresponds to the first TRUE condition. IFS can take the place of multiple nested IF statements, and is much easier to read with multiple conditions.
An example of usage follows:
=IFS(A2>89,"A",A2>79,"B",A2>69,"C",A2>59,"D",TRUE,"F")
You can even specify a default result:
To specify a default result, enter TRUE for your final logical_test argument. If none of the other conditions are met, the corresponding value will be returned.
The default result feature is included in the example shown above.
You can read more about it on Microsoft's Support Documentation

Resources