I'm trying to use the IF function in Excel so that if the first character of a cell is 9, then the value shown should be the eight rightmost characters of that cell, otherwise the value shown should be the four rightmost characters. This formula however does not work:
=IF(LEFT(A2,1)=9,RIGHT(A2,8),RIGHT(A2,4))
It keeps returning the rightmost four numbers even though the number in cell A2 starts with 9.
Could you please point out what I'm doing wrong here?
LEFTreturns text, so the comparison needs to also be against a string:
=IF(LEFT(A2,1)="9",RIGHT(A2,8),RIGHT(A2,4))
or you need to convert the result of LEFT to a number again:
=IF(NUMBERVALUE(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
try =IF(INT(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
A shorter version if the condition is used to set the number of characters:
=RIGHT(A2,4+4*(LEFT(A2)="9"))
Related
Imagine A1 is a cell that I put numbers in and A2 is a cell that will automatically give the vaule of A1 divided by 5.
=A1/5
But when I enter a number to A1 with a greater than symbol (>) or a less than symbol (<) such as >15, A2 gives #VALUE! instead of >3. Is there a way to code this please?
I can't comment yet but I'd like to add an explanation to Jos Woolley's answer
=IF(N(A1),A1/5,LEFT(A1)&MID(A1,2,99)/5)
N(A1) Is using the property of the N function to check if the value in A1 is a number or, in your case, a number with an operator in front of it. If the value of A1 is a whole number, A1/5 is produced, and if A1 has an operator (</>) then LEFT(A1)&MID(A1,2,99)/5 will be produced.
The LEFT(text, [num_chars]) function takes a cell value and recreates that cell, from the left, for a total number of characters put into the function in the [num_chars] parameter. If no parameter is set, the single leftmost character will be used, in you case, it will be < or >.
& is used to string together calculated cells, and in this case is being used to take the < or > from the LEFT function and adding the MID function to it.
The MID function is like the LEFT function, only you can choose the starting character, rather than start at the beginning of the string. What's happening in MID(A1,2,99) is taking the value in A1, for example >15, and starting at the second character, recreates the next 99 characters. This means as long as the number you're using to divide is 99 digits long or less, this formula will work fine. This value is then divided by 5.
I hope that's a good enough explanation, since you said you didn't understand what it meant at first. Glad that it works for you!
Also, because of your comment, I'm assuming it looks something more like
=IF(N(A1),A1/5,LEFT(A1)&ROUND(MID(A1,2,99)/5,1))
Which would indeed get you your result :)
=IF(N(A1),A1/5,LEFT(A1)&MID(A1,2,99)/5)
I have this content in my cell in Excel 'TS_Bud03A'
I have other cell content with content that should not be touched by bullet no 1 below. For instance cells with P_Har02b
I would like one nested formula for 2 operations
Substitute 'TS_' with 'TVS_'
Remove the last letter after the 2 digit number if one exists
For the first operation I have made this formula
=SUBSTITUTE(K12;"TS_";"TVS_")
It works. But how do I nest another substitute, that will remove the last letter (any case - if it exists) after the 2 digit number?
By combining the 2 answers below I think I almost got it. Like so:
=SUBSTITUTE(REPLACE(LEFT(K15;LEN(K15)--ISNUMBER(RIGHT(K15;1)*1)-1);2;0;"V");"TS_";"TVS_")
But it still changes this P_Bud04a to this PV_Bud04 and this is wrong. The P cells must be untouched.
Any ideas how I can remove the replace that adds V part of the formula I have here in the question?
See if the following works for you:
Formula in B1:
=REPLACE(LEFT(A1,LEN(A1)--ISNUMBER(RIGHT(A1,1)*1)-1),2,0,"V")
REPLACE(<string>,2,0,"V") - Acts like pasting a substring into a string.
LEFT(A1,LEN(A1)-<number> - Our string in the first parameter of REPLACE() has to be either the full length or minus one. That number is still to be determined.
-ISNUMBER(RIGHT(A1,1)*1)-1 - Will check if the last character in A1 is numeric and therfor will return a boolean value TRUE or FALSE. Their equivalent numeric values are 1 and 0. So if TRUE it will return 0, if FALSE, it will return 1.
EDIT: After your comment I understand your data does not always start with TS_, therefor you need to simply swap REPLACE() with SUBSTITUTE(), e.g:
=SUBSTITUTE(LEFT(A1,LEN(A1)--ISNUMBER(RIGHT(A1,1)*1)-1),"TS_","TVS_")
=IF(ISNUMBER(--RIGHT(K12;2));SUBSTITUTE(LEFT(K12;LEN(K12)-2);"TS_";"TVS_");SUBSTITUTE(K12;"TS_";"TVS_"))
I think this is what you need. It first checks if the right last 2 characters are a number (added -- to convert number stored as text to number). Then, if true, remove the last 2 characters and substitute like you mentioned, or, if false substitute according to your formula without removing the last 2 characters.
OK I got it. I just removed the V between quotes, so as to not put any new content into the cell. And it worked.
=SUBSTITUTE(REPLACE(LEFT(K12;LEN(K12)--ISNUMBER(RIGHT(K12;1)*1)-1);2;0;"");"TS_";"TVS_")
Thanks guys
I have a match function that I could not get working. I boiled it down to the point that it can't find the appropriate match since the values are not the same, apparently.
I have the value 21337 in cell D59. In cell S59 I have the function: Right($d59;5), which displays 21337. However when I enter in a cell: =D59=S59 i get the return FALSE.
I use the Right() function because cells in column D contain concatenated values, where the last 5 values are of importance. For example D60 contains 21337 - 21448, where 21448 is the value I want to match.
Anyone has a clue on what might be the problem?
With no formatting you'll see that 21337 is right aligned - showing this is a number and treated as a number by Excel.
On the other hand Right($d59;5) will show the number left aligned, indicating that the returned value is being treated as text by Excel.
If you try Right($d59;5)*1 Excel will implicitly convert the value back to a number (due to the calculation performed) and both values will be equal.
To be explicit about the conversion, as Brian has pointed out, use VALUE(Right($d59;5)).
If you use "Formula" > "Evaluate Formula", does it show the penultimate Evaluation as21337="21337"
The LEFT(..) function will convert the number to a string, and the string and the number will not equate. Try either =TEXT(D59,"#")=S59 or =D59=N(Left(S59)) to convert in your comparison, or change the code in S59 to =N(Right($D59,5)) to make S59 show a number
(The N(..) function converts a string to a number, returns 0 if Not A Number)
I've got a fairly simple question and I'm sure that I'm missing something obvious.
I've got say 40 cells and all of them contain a formula in them. Only 38 of those cells actually have string or text in them the remaining two do NOT. They're blank with the exception of the formula.
However when I do a COUNTIF or a COUNTA to try and not count the cells that are filled automatically it is giving me the result of 40.
Ways I've tried this and all go the result of 40:
=COUNTIF(B60:B99,"*")
=COUNTIF(B60:B99)
=(COUNTA(B60-B99)
Does anyone know what I'm doing wrong?
Example of the formula in a blank cell that is being counted:
=IF(ISBLANK('Dodgeball'!B48),"",'Dodgeball'!B48)
Use:
=SUMPRODUCT(--(B60:B99<>""))
as this ignores null strings.
You are attempting to count the cells that are not "" but considering them as blank. Try using
=countif(B60:B99,"<>""""")
To explain that final string of quotes, the first one is an escape character so that the second one is read as quotes within the string, similarly the third and fourth are an escape character and a quote for within the string, and finally we end the string with a quote.
One option would be to insert an additional column and simply use the istext() function. Then you can sum that column to get your text count, because false counts and 0 and true counts as 1.
A program that exports to Excel creates a file with an indented list in a single column like this:
Column A
First Text
Second Text
Third Text
Fourth Text
Fifth Text
How can I create a function in excel that counts the number of white spaces before the string of text?
So as to return: 1 for the first text row and 3 for the for the thirst row, etc in this example.
Preferably seeking a non-VBA solution.
TRIM doesn't help here because it removes double spaces also between words.
The main idea is to find the FIRST letter in the trimmed string and find its position in the original string:
=FIND(LEFT(TRIM(A1),1),A1)-1
You can try this function in Ms Excel itself:
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))
This would apply if the results are in a single cell. If it is for a whole row/column, just drag the formula accordingly.
Try below:
=FIND(" ",A1,1)-1
It calculates the position of the first found whitespace character in a cell and reduces it by 1 to reflect number of characters before that position.
As per http://www.mrexcel.com/forum/excel-questions/61485-counting-spaces.html, you may try:
=LEN(Cell)-LEN(SUBSTITUTE(Cell," ",""))
where Cell is your target cell (i.e. A1, B1, D3, etc.).
My example:
B8: =LEN(F8)-LEN(SUBSTITUTE(F8," ",""))
F8: [ this is a test ]
produces 4 in B8.
The above method will count spaces before the string if any were inserted, between individual words and after the string, if any were inserted. It won't count available space that does not have an actual white space character. So, if I inserted two spaces after test in the above example, the total count would be raised to 6.
As has been pointed out in the other answers, you can't really use TRIM or SUBSTITUTE as potential spaces in between words or at the end will give you the wrong result.
However, this formula will work:
=MATCH(TRUE,MID(A1,COLUMN($A$1:$J$1),1)<>" ",0)-1
You need to enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
In case you expect more than 10 spaces, replace the $J with a column letter further down in the alphabet!
Here's my solution. If the left 5 characters equals "_____" (5 blank spaces), then return 5, else look for 4 spaces, and so on.
=IF(LEFT(B1,5)=" ",5,IF(LEFT(B1,4)=" ",4,IF(LEFT(B1,3)=" ",3,IF(LEFT(B1,2)=" ",2,1))))
You almost got it with LEN + TRIM in answers before, you only need to combine both:
=LEN(Cell)-LEN(TRIM(Cell))
If it is Indented you could create a Personal Function like this:
Function IndentLevel(Cell As Range)
'This function returns the indentation of a cell content
Application.Volatile
'With "Application.Volatile" you can make sure, that the function will be
recalculated once the worksheet is recalculated
'for example, when you press F9 (Windows) or press enter in a cell
IndentLevel = Cell.IndentLevel
'Return the IndentLevel
End Function
This will work only if it is Indented, you can see this property in the Cell Format -> Alignment.
After This you could see the Indentation Level.