Combining trim and if formula - excel

I am using a trim formula in excel:
TRIM(LEFT(SUBSTITUTE(MID(M2,FIND("|",SUBSTITUTE(M2,"-","|",2))+1,LEN(M2)),"-",REPT(" ",LEN(M2))),LEN(M2)))
this looks for a number in between a string of 4 numbers:
193449542-27309370502-9045796-169794419204
which works perfectly.
I want to add an if= when a another cell on that same row contains either "bing" or "Adwords" and skip cells that contain it.

In H2 per the supplied image,
=IF(NOT(SUM(COUNTIF(J2, "*"&{"bing","Adwords"}&"*"))), TRIM(LEFT(SUBSTITUTE(MID(M2,FIND("|",SUBSTITUTE(M2,"-","|",2))+1,LEN(M2)),"-",REPT(" ",LEN(M2))),LEN(M2))), "")
To add a condition where M2 must contain at least three hyphens, use AND and subtract the length of substituting the hyphens with "" (subtrahend) from the length of the original (minuend). If the result (difference) is 3 or more then there are at least three hyphens.
=IF(and((len(m2)-len(substitute(m2, "-", "")))>=3,NOT(SUM(COUNTIF(J2, "*"&{"bing","Adwords"}&"*")))), TRIM(LEFT(SUBSTITUTE(MID(M2,FIND("|",SUBSTITUTE(M2,"-","|",2))+1,LEN(M2)),"-",REPT(" ",LEN(M2))),LEN(M2))), "")

Related

Removing 1st character of number before checking if its > 0, combined with indirect function

I would like to use the =indirect() and =value() function together. I am to count how many numbers are > 0 in another sheet, in the rows I:I. In these sheets, the numbers are not formatted as values, and I am not to do this manually.
In addition, for the numbers, they are formatted as
" 123 "
With a space before the number.
I also need to remove the space in front the numbers in order for this to work.
In my current sheet, in cell J1, I have written "I:I". In the cell A3, I have the name of the sheet that I would like to count the numbers from.
My attempt so far:
{=COUNT.IF(INDIRECT("'"&A3&"'!"&$J$1);">0")
However, this just returns values 0. This is because there is a space infront of the numbers. How do I remove this space? I need to implement it in this function
I've edited the post a bit, as I noticed there was a space infront of the numbers
On a worksheet, if your value " 123 " is in cell A1 then you could use a formula like:
=VALUE(TRIM(A1))
...to TRIM the spaces, and convert the string to a numeric VALUE.
Or, in VBA, something like:
myValue = CInt(Trim(myString))
...to Trim the spaces, and Convert to CInt-eger.
Or, in many ad-hoc situations it's easiest to use "Text to Columns" to remove padded spaces (or any other character, really).
More ways to convert text to numbers here and here.
More ways to remove unneeded spaces here and here.

Total occurrences of a single digit from a string of digits across a range of cells in a column

We need to count how many occurrences of each number are in a cell over a range of cells in the same column and output a tally of the totals for each number. See the attached picture and the desired output in the column next to it. We tried other formulas found online in both excel and open office with no results.
letter Count
Working Count
Try the following formula in D1:
=LEN(TEXTJOIN("",TRUE,A:A,"#"))-LEN(SUBSTITUTE(TEXTJOIN("",TRUE,A:A,"#"),C1,""))
and populate down.
(you will need 2016 or later for TEXTJOIN)
Option 1
Single array formula (ctrl+shift+enter !) which will work for strings with a maximum length of [5] alphanumeric characters (but you can easily modify the formula by adding a few numbers in the hard-coded array constant {1,2,3,4,5}):
{=SUM(N(MID($A$1:$A$500,{1,2,3,4,5},1)=TEXT(C3,"#")))}
You can add some further trickery to let Excel define the array constant, so the formula will work for any length of the string of digits :
{=SUM(N(MID($A$1:$A$500,
COLUMN(INDIRECT("A1:"&CHAR(65+MAX(LEN($A$1:$A$500)))&"1"))
,1)=TEXT(C3,"#")))}
The part in the middle (COLUMN()) creates the {1,2,3,4,5} array. You might have seen other versions of this formula, without the CHAR, which I use to create a reference to e.g. cell E1 (knowing that 65 is the code for "A").
.
Option 2
This array formula (ctrl+shift+enter !) works in all Excel versions, but is not very "elegant" as you have to repeat the key part of the formula as many times as the maximum digits you have in your cells (this example is for max 3 characters):
{=SUM(
N(MID($A$1:$A$500;1;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;2;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;3;1)=TEXT(C3;"#")) )}
The character you are counting is in C3. This will work for numbers and letters. If you can have five alphanumeric characters, you have to add two more N(...) parts, and replace the second parameter of the MID function with 4 and 5 respectively.

multiple selection drop down list

I would like to count the number of unique selections from a column that contains multiple selections from a drop down list.
For example, column B3 contains,
Monday, Tuesday, Wednesday
The count function returns a value of 1 instead of 3 - is there any way to count the three days distinctly? Thank you!
Multiple selections from a drop down list was made possible using the VBA code from: https://docs.google.com/document/d/1JU7G_Tna2zPBtcG2TlarxKCTbuinNsg5LwBqzmuJYK8/edit
This solution is contingent on your string values always being separated by a comma (,). It appears that the code you shared in the link will continue to add comas with the addition of each new string, which means this should work for you.
=IF(A1="",0,LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1),",",""))+1)
If cell is blank, return 0
Else, Count instances of commas (,) and offset by 1
Edit 1: (Explanation)
Notice the equation has two very similar parts: LEN(TRIM(A1)) & LEN(SUBSTITUTE(TRIM(A1),",","")). The only difference is that one equation uses the Substitute function and the other does not.
Trim will remove leading and lagging spaces
Len returns the character count of your string
Substitute, in this instance, is used to replace (substitute) all commas with a blank string ("")
The difference between the length of the string with commas to the length of the string without commas equals the total commas that are present in the string. Saying to "count the commas" was a little misleading on my part since we are really deducing the number of commas.

How to extract a string from a cell from right to left using a specific character that occurs more than once?

Normally I would just use the RIGHT function in excel to split it by finding a specific character such as / and outputting the string that I want.
However, I am finding trouble extracting THISSTRING.txt from d/aaa/THISSTRING.txt. With only one instance of / I would just use a function such as =RIGHT(B17,LEN(B17) - FIND("/",B17))
Here's one way to get the rightmost:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))
Objective: To return the rightmost sub-string from a target string after the last occurrence of a character which appears several times within the target string.
This formula:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))
Provides the correct result under the following conditions:
The sub-string to retrieve does not have more than 99 characters.
The sub-string to retrieve does not contain more than one space character together.
Example: To retrieve a sub-string which is 123 characters long and contains the following characters 1 ABC XXX 123 XYZ.
Point 1 is easily solved by working with the length of the string instead of a fixed number:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",LEN(A1))), LEN(A1)))
However point 2 can't be overcome with the referred formula.
Proposed solution: The following formula returns the correct result regardless of the conditions mentioned above:
=RIGHT(A1, LEN(A1) - FIND( CHAR(12),
SUBSTITUTE(A1, "/", CHAR(12), LEN(A1) - LEN( SUBSTITUTE(A1, "/", "" )))))
Note: I used non-printable character 12 which is very unlikely to be found in excel, change as required.
Here's another way.....
=REPLACE(B17,1,LOOKUP(2^15,FIND("/",B17,ROW(INDIRECT("1:"&LEN(B17))))),"")
FIND generates an array of values, indicating the first position of a "/" in B17, but with the start point incrementing by 1 each time, which means that the last numeric value in that array is the positon of the last "/".
LOOKUP extracts that value from the array and we can use it in REPLACE function to replace all characters before and at that position with nothing, just leaving you with all characters after the last "/"
You'll get an error if there is no "/" in B17

How to extract parsed data from once cell to another

Given a spreadsheet cell containing a string that consists of a hyphenated series of character segments, I need to extract the last segment.
For example, consider column A containing data strings like XX-XXX-X-XX-XX-G10, where X denotes any character. What formula would I need to place in column B to get G10 as a result?
A B
1 XX-XXX-X-XX-XX-G10 G10
I'm looking for a formula that could work in in Libre Office Calc, Open Office Calc, MS Excel, or Google Sheets.
Another possibility in LO Calc is to use the general purpose regular expression macro shown here: https://superuser.com/a/1072196/541756. Then the cell formula would be similar to JPV's answer:
=REFIND(A1,"([^-]+$)")
If you are using google sheets, regexextract would be possible too:
=REGEXEXTRACT(A1, "[^-]+$")
In LibreOffice Calc and OpenOffice Calc, you can use a regular expression to determine the position of the text after the last - character:
=SEARCH("-[:alnum:]+$";A1)
will return 15 if A1 contains XX-XXX-X-XX-XX-G10.
Now, you can use this value to get the text "behind" that position, using the RIGHT() function:
=RIGHT(A1;LEN(A1)-SEARCH("-[:alnum:]+$";A1))
Split up on multiple lines:
=RIGHT( ' return text beginning from the right...
A1; ' of cell A1 ...
LEN(A1) ' start at lenght(A1) = 18
- ' minus ...
SEARCH( ' position ...
"-[:alnum:]+$" ' of last "-" ...
;A1 ' in cell A1 = 15 ==> last three characters
)
)
It appears that you want the characters that appear at the end of a string, to the right of the last instance of a hyphen character, "-".
This formula, adapted from here, works in Excel, *Calc & Google Sheets:
=TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),LEN(A1)))
Explanation:
SUBSTITUTE(A1,"-",new_string) will find each hyphen ("-") in the original string from cell A1 and replace it with a new_string.
REPT(" ",LEN(A1)) is a string of repeated space characters (" "), the same length as the original string in cell A1.
TRIM(RIGHT(string,count)) will get the right-most count characters, and trim off leading and trailing spaces. Since the string was previously padded out by replacing hyphens with spaces, and count is the same LEN(A1) used for that padding, the last count characters consists of a bunch of spaces followed by whatever followed the last hyphen!
In Google Sheets, an alternative approach is to use the SPLIT function to break the value from column A into an array, then select the last element. (Excel-VBA has a split() function, so you could make this work in Excel by writing VBA code to provide it as a custom function.)
=INDEX(SPLIT(A1,"-"),0,COUNTA(SPLIT(A1,"-")))
I found simply solution:
=RIGHT(A1;3)
that gives me G10 as the result too! It works because COL A always have 3 chars at the end!

Resources