When I was handeling a text-comparison in my database I noticed some odd behavior. In my database I want to SUM values based on a column containing strings. In this example, I want to make a group str and a group txt, with the following string values.
str 2
str1 1
str2 0
txt 1
txt1 2
tx2 3
If I would compare the text with a simpel boolean, i.e. =("str"="str*"), it returns False, because the * is an additional character. This makes sense in some way. However, when I used two other techniques the comparison is handled differently:
First, the following simple SUMIFS function:
=SUMIFS(B:B;A:A;"str*") and =SUMIFS(B:B;A:A;"txt*") includes the values at "str" and "txt" respectively, suggesting the comparison is True.
Second, =Match("str*";{Cell containing "str"};0) returns 1, indicating that the comparison also returns True.
Why does the boolean string comparison return False, whilst MATCH and SUMIFS assume True?
In my opinion those two functions apply the wildcard interpretation of the asterisks while your direct comparison formula with '=' takes it as just another (extra) character in the string inside the quotes, so making the two strings different.
While in the function Match():
If match_type is 0 and lookup_value is text, lookup_value can contain the wildcard characters asterisk (*) and question mark (?). An asterisk matches any sequence of characters; a question mark matches any single character.
I hope this makes sense.
Only a limited number of Excel worksheet functions can use wildcard characters to filter results. Functions like COUNTIF ,VLOOKUP, MATCH and others as listed here are some Excel functions that use wildcards.
Apart from the functions listed in the link, wildcard * is treated like a literal when used in the double quotes.
Related
I'm using VLOOKUP but the Lookup_Value I have is a number and the Table_Array I'm looking up has the number inside text; i.e. I'm looking for '200' but the search column is 'abc200a' or 'abcd200 Aa 10'.
How do I obtain a match despite the surrounding text, since trim functions only trim n characters left or right?
Thanks
I have a long string in excel. I need to check if every char of the string match an array of char.
For example:
Array of allowed char: "i a m n o t I t e r s d *space*" --> CHECK OK
"I am not interested" --> check ok
"I am not very interested" --> check failed : v and y non allowed
I have tried with find or match but they just look for the first occureence,....
For a case-sensitive approach, if you have the CONCAT function, you can use the following:
(For case-insensitive, replace FIND with SEARCH)
=IF(OR(ISERR(FIND(MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1),1)),1),CONCAT(" ",AllowedChars)))),"check failed", "check ok")
If you do not have the CONCAT function, replace it, in the formula, with a simple string of the allowed characters:
=IF(OR(ISERR(FIND(MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1),1)),1)," iamnotItersd"))),"check failed", "check ok")
and note that some earlier versions of Excel will require that you confirm this array formula by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula seen in the formula bar.
the MID(… function creates an array of the individual characters in the test string
FIND then does a case-sensitive FIND of all the characters in test string against the characters in AllowedChars
FIND will return a #VALUE! error if the character is not found.
OR(ISERR(… will return TRUE if there are any failures to match.
Edit further explanation:
ISERR(… will return an array of Booleans {FALSE,FALSE,FALSE,FALSE,TRUE, … } depending on whether each FIND is returning a number of the #VALUE! error
OR evaluates that array and returns TRUE if there is any error at all.
Where AllowedChars refers to the range storing the allowed characters.
CONCAT ignores empty cells in a range, so we have to add the space as one of the arguments.
With a UNIQUE list of acceptable characters in a range(I used D1:D10) one can compare the sum of the difference of removing each character and the length of the string without spaces:
=SUMPRODUCT(LEN(A1)-LEN(SUBSTITUTE(A1,$D$1:$D$10,"")))+1 = LEN(SUBSTITUTE(A1," ",""))
This formula ignores case and the list must be a unique list of allowed characters. I and i are the same as Excel sees it and only one can be in the 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.
I am trying to match HA24BB-3-1LL with HA24B*-3-1** in Excel. Another example is matching HA24FB-3-1LL with HA24F*-3-1**.
However, when I performed the regular match function, these could not be matched.
col A col B
1 HA24BB-3-1LL HA24F*-3-1**
2 HA24FB-3-1LL HA24B*-3-1**
What I tried:
=MATCH(A1,B:B,0)
It should return 2 but it returns #N/A.
May I know why?
I thought Excel match function works with wildcard. Is there a way to enable it?
You can match with wildcards, but the wildcards have to be in your lookup value (first position in the formula). If they are in the lookup array (second position in the formula) they are not wildcards, just literal *s in the cell values.
So you can find matches to strings like HA24B*-3-1** in your first column by using the formula: =MATCH(B1,A:A,0), but not the other way around, as your formula is set up.
Also, if you are looking for things that match HA24B[one character]-3-1[two characters] your search string should instead be HA24B?-3-1??. The * will match a string of any length, so it is redundant to put two of them at the end of your search string, and using them will also find you matches to strings like HA24Babcdedfghijklmnopqrstuvwxyz-3-1abcdefghijklmnopqrstuvwxyz. Which may be what you want, and if it is leave it as is (minus the second * at the end). The ? matches a single character, which I am assuming is what you are looking for since you used ** in your question.
I am using an array formula (in Excel 2003) to count the number of strings meeting a specific condition. I am using a column of strings instead of a table of cells to store my data because of file size limitations.
The formula is below:
{=SUM(IF((VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),6,1))*VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),1,1)))=VLOOKUP(D2,t.lkup,2,FALSE),1,0))}
The expression VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),6,1)) looks through the cells in a named range to return a value. This value is multiplied by another value returned by the expression VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),1,1)). The resulting product is then looked for in a set of numbers given by VLOOKUP(D2,t.lkup,2,FALSE), which returns a string like "{1,2,3,4}". If the product is an element of set, then 1 is added to the sum, else 0 is added to the sum.
When I use the array formula above (with the dynamic lookup set), a value of zero is returned. If I use the following formula,
{=SUM(IF((VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),6,1))*VALUE(MID(INDIRECT(CONCATENATE(D1,"test")),1,1)))={1,2,3,4},1,0))}
then the correct sum is returned. Does anyone know how to get Excel to treat the set lookup as a static set instead of a string?
An array formula performs multiple clalculations and returns either a single result or an array. All array arguments in the formula must be of equal size.
in your first example you compare a single value of something (as the most outer function within IF is a VALUE() function) to a VLOOKUP which returns a string ... that must go wrong ... the fact that your string contains curly brackets does not convert it into an array.
in your second example you compare a single value to an array containing elements {1,2,3,4}, so actually you do four comparisons, and if one of them resolves to TRUE you add 1.
I don't know any way to convert a comma delimited string "{1,2,3,4}" into an array {1,2,3,4} without use of VBA, but maybe you can change your tactic by converting your VALUE(...)*VALUE(...) number into a string and use the FIND() function to identify number as a substring of the condition string.
e.g. say your MID_CONCATENATE_TIMES_BLA_BLAH results in 7, and your
condition string = "{1, 3, 5, 7, 9}", a FIND(MID_BLA, CONX_STR) = TRUE
condition string = "{1, 2, 3, 4}", a FIND(MID_BLA, CONDX_STR) = FALSE
This will work as long as your results are 1 digit. With more than 1 digit you would need to include a SPACE before the number in both MID_BLA and CONDX_STR; also not too difficult, but adding even more complexity to the formulae.
If you want to do VBA, you can use the Split() function to create a zero-based array from a seperated string
Function StrToArray(Arg As String) As Variant
StrToArray = Split(Arg, ",")
End Function
and surrond your VLOOKUP in (1) by StrToArray(VLOOKUP(...)), in which case you must remove the curly braces from your condition strings.
Hope that helps - good luck