Excel Formula with if, then - excel

I need to write a formula in Excel that performs the following:
If cell A1 contains the text ".png",".jpeg" then cell B1 = Image
If cell A2 contains the text ".mov",".mp4" then cell B2 = Video
And so on...
Also, is it possible to apply this validation with additional validation already applied? Like a list.

Solution
I broke the solution to parts so you won't be shocked by a long line of formula.
What I need to get it to work
Extract the suffix of a string, I've done that using the length of the string in column B and then putting it in column C.
B2=LEN(A2)
C2=IFERROR(RIGHT(A2,LEN(A2)-SEARCH(".",A2)),$J$4)
The IFERROR is in case the file doesn't contain a dot and then column C will show the message in cell $J$4.
Get the type by a so called "database" table which I've built to map which suffix belongs to which type of file
D2=IFERROR(INDEX($F$2:$G$8,MATCH(C2,$F$2:$F$8,0),2),$J$6)
I've used the INDEX and MATCH functions to fetch the data from the database.
The IFERROR in this case means - I didn't find the desired prefix in the Database table (in $F$2:$G$8)
After filling these formulas, drag down the data to the rest of the cells and you'll get the desired results.
Here's an image of my Excel file and below it you'll see the cells mapping to prevent you from guessing where is what.
Cells Mapping
Database = $F$2:$G$8
No Dot string = $J$4
Not a valid suffix string = $J$6

=IF(
OR(ISNUMBER(SEARCH(A1,".png")),ISNUMBER(SEARCH(A1,".jpeg")))
,"Image"
,IF(
OR(ISNUMBER(SEARCH(A2,".mov")),ISNUMBER(SEARCH(A2,".mp4")))
,"Video"
,"")
)
Please check for the closing brackets yourself as I just typed this off my head.
That formular should be used for cell B1

Related

Find common text within a range of cells(range containing blanks as well)

This is the problem i am facing in Excel formula
enter image description here
In column F, i want to find the common text across A2 to E2 (containing Blanks)
My Question:
Is there a simple way to get the result without VB?
Any help is appreciated,thanks
I found that google sheets has some really cool functions.
If you put the formula =SPLIT(A1, ",", TRUE,FALSE) in the cell after your row of common text (or probably even in a different sheet - "probably because hadn't tried it, though it should), the next x cells (where x is the number of "," in A1 - because "," is the delimitator) will be the text.
then you can put the code =IF(SUM(ARRAYFORMULA(if(REGEXMATCH($A$1:$D$1,F1),1,0)))=COUNTA($A$1:$D$1),F1,"") into an equal number of cells after that (probably should just put into the max number), and =CONCATENATE(I1:L1) into the last cell.
Ok. So to tweak this for yourself: I found that ARRAYFORMULA lets you put an array in place of a single cell in a function inside. how it exactly works I read its like a for loop. but I can't really vouch for that. but here it lets you have REGEXMATCH (which is a Boolean check on the cell you give it for if it contains the given REGEX) check each cell in the array.
the sum will add them up, and the if will match against the COUNTA to find if the number of cells in the array that contain this string is equal to the number of non-empty cells.
the concatenate at the end adds all the cells (containing the regex function) together, and since the only non-empty cells will be the one with the string, that is what this cell will return (no spaces).
code:
results:
the test data:
If you need in specifically Excel... this won't help.
We can use power query to achieve the desired result.
Unpivot the columns in Power query
Split all the columns by Comma delimiter
Create a custom column to see if the first column records exist in the remaining columns.
Use the functionText.contains.
Sample function: =Text.Contains([column.1],[column.1]&[column.2]&[column.3])
If the above function returns TRUE then get the first column result(This is the expected result) and load the data back to your excel

Extracting strings between specific characters in excel and separating them with a comma

Is there a way to extract multiple strings between specific characters in excel and separating them with a comma.
For example:
I am thankful for every help!
The following requires a version of Excel O365 that supports dynamic arrays and the LET function.
If I understand correctly you are looking for something like this.
This formula will list all the positions of all [ in the string using dynamic array functions.
=LET(x,$B$2,
y, SEQUENCE(LEN(x)),
raw, IF(MID(x,y,1)="[",y,""),
filtered, FILTER(raw,raw<>"",""),
filtered)
LET allows you to set names within a formula using parameter pairs. The first is the name; the second is the value of the name. The last parameter is the value returned. In this case, x is set to cell B2. y is set to the array listing the numbers from 1 to the length of x using the dynamic array function SEQUENCE. raw is a list that shows the value of y if that position is [, otherwise it is blank. filtered uses the FILTER function to remove all of the blank rows. filtered is the last argument of the LET function so that is what is returned. If this formula is entered into cell A4 then A4 will show 1, A5 will show 14 and A6 will show 28.
If you then enter a similar formula in B4 replacing [ with ] then the result is {7,21,35} in cells B4, B5 and B6.
Finally, in cell B2 you can enter =TEXTJOIN(", ",TRUE,MID(A2,A4#+1,B4#-A4#-1)). This will return the result you are looking for.
If you are unfamiliar with dynamic arrays, A4# indicates an entire dynamic array starting in cell A4. In this case, it is the same as A4:A6.
More information on Dynamic Arrays
More information on LET
You can't do it with only formulas in Excel 2007. You need VBA UDF then. With Excel O365 having dynamic formula access, this can be done like:
=TEXTJOIN(", ",TRUE,TRIM(LEFT(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A1,"]",REPT(" ",100)),"[","</s><s> ")&"</s></t>","//s[starts-with(., ' ')]"),100)))
Considering that you are using too old version of excel which lacks dynamic array functions and besides too many other useful functions, You may probably have to do a long workaround here.
First using substitute and len You have to find out number of such square parenthesis in each of the row. Thereafter you have to work out the formula of max of such numbers.
Do it like this
Assuming your text-values in A2 for a max of 4 occurrences enter the following formula in B2
=SUBSTITUTE(TRIM(REPLACE(LEFT(A2,FIND("]",A2&"]")-1),1,FIND("[",A2&"["),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",2),"]","#",2))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",2),"[","#",2)),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",3),"]","#",3))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",3),"[","#",3)),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",4),"]","#",4))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",4),"[","#",4)),"")), " ", ", ")
Let's say this is the text in A2
I have a text [123] and some more [4523] and also [552222] how to extract [22]?
This will create a output of 123,4523,552222,22 in B2
There is no "extraction" in your question, just removal. The formula below replaces the unwanted characters with "", thereby removing them. Please try it.
=SUBSTITUTE(SUBSTITUTE(A2,"[",""),"]","")

=ISNUMBER(SEARCH()) formula not working properly

Basically, im trying to search if values from column b is contained in cells on column a
I am currently using the formula
=ISNUMBER(SEARCH(B1,$A:$A))
and using it inside a conditional formatting to highlight the cells in column A that contains strings from column B. But it is not highlighting the correct cells
any advice?
Problem is that your ISNUMBER(SEARCH(…. formula is returning an array of values {FALSE;TRUE;FALSE;FALSE;...} one return for each item in within_text. You need to know if any of those items match.
So, with your formula, consider the array formula modification
=OR(ISNUMBER(SEARCH(B1,$A:$A)))
Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar
If you don't like to use the CSE entry method, you could use this formula which will return zero for no matches, or be non-zero for any matches:
=SUMPRODUCT(-ISNUMBER(SEARCH(B1,$A:$A)))
Excel's SEARCH function is used to find the position of one string within another string. Generally you use it like this:
=SEARCH("String A", "A Longer String Containing String A")
This will return the character index where first string starts within the second string, which in this case would be 28.
What you really need is a VLOOKUP. Since you're doing a textual search (substring), you need your range to be of text type instead of number.
You should do the following:
Add an extra column to the right of Column A and use TEXT function to convert entries to textual form:
=TEXT(A1, "#")
Now you can use VLOOKUP to perform a substring-match in this textual range. VLOOKUP supports wildcards when you do not ask it to perform an exact match (4th argument should be FALSE). Here is your formula then:
=VLOOKUP("*" & C1 & "*",$B:$B,1,FALSE)
Note that I have passed column B (textual column) as the lookup range, whereas C1 is the cell containing the text that you want to search.
This method also has the additional advantage that it returns the actual matched entry from the range so you don't have to find it manually.
Once you have your results, you can apply conditional formatting to it.
Highlight column A (or the relevant range in column A starting cell A1) with the first cell (which is A1 in this case) as the active cell, use the following formula as the conditional formatting rule:
=(SEARCH($B1,$A1)*(LEN($B1)>0))>0
The logic is to first search the given sub-string from the main string, then multiple the result by LEN($B1)>0 to exclude the result of 1 returned for blank cells in column B.
Note: Conditional Formatting works in array fashion so even though the formula only looks at values in the first row of the range, as long as you use the relative (or in some cases absolute) cell references correctly and highlight the result range correctly before setting up the rule, the rule will be applied across in the same way as for the first row of the array as demonstrated in this example.

Problem extracting data from a cell populated from a formula

I imported a txt document which creates 7 columns of data. One of the data points in the document is a MAC address, however, due to the format of the txt document (and there is no way around this), the MAC address is split up into 6 columns (B-G), with all other pertinent data (non MAC addresses) existing in column B.
I am trying to write a formula to check a cell in column B, and if it contains "BSSID" then it will combine the text in the corresponding row from columns B-G and enters the new value in column H (so it shows as a normal MAC address). If the cell does not contain "BSSID", then the value of that cell just needs to be moved to the corresponding row in column H.
MY PROBLEM IS given the formula below, if the cell contains "BSSID", the corresponding row in column H will only display the value of the cell in the first column, instead of all the columns.
I have tried taking the code, that combines cells in B-G within the formula, and surrounding it in brackets and quotations, with no luck.
I also tried making this a multiple step solution by only running the formula to combine everything in column H, and then in column I, via a formula.
I tried to move the value returned in column H to column I, but I run into the same issue.
And I have tried swapping the return values, just to make sure, I didn't mix up the true return with the false return.
Original Code I would like to get to work:
=IF(ISNUMBER(SEARCH(“BSSID”,A2)),B2&":"&C2&":"&D2&":"&E2&":"&F2&":"&G2,B2)
This is what the code looked like, when I broke it into 2 parts:
Column H: =B2&":"&C2&":"&D2&":"&E2&":"&F2&":"&G2, B2
Column I: =IF(ISNUMBER(SEARCH(“BSSID”,A2)),H2,B2)
Both codes only return the value in cell B2 if true, instead of what should look like a MAC address.
My expected results would be, in a single formula, if B2 contains the string "BSSID" that H2 would show the content of B2-G2 formatted to look like a MAC address; and if B2 does not contain the string "BSSID" then H2 will show the content of B2.
Actual result is that H2, when the formula returns true, only displays B2 and not B2-G2.
I would approach this problem as follows:
Check the cell for BSSID using an IF statement =IF(SEARCH("BSSID",A2), <true>, <false>)
This statement may result in an error though, if "BSSID" isn't found. Your code looks to be fine, but perhaps herein lies the issue. To be sure, we can insert a catch for the errors using IFERROR =IF(IFERROR(SEARCH("BSSID",A2), FALSE), <true>, <false>)
Then, within the <true> section of the IF statement, I would use TEXTJOIN to combine my cells with a colon inbetween ...TEXTJOIN(":",TRUE,B2:G2)...
EDIT: I notice that you say in one location that you are checking cell A2 for "BSSID" and in another you say you are checking cell B2. Perhaps make sure you aren't checking the wrong cell?
=IF(ISNUMBER(SEARCH(“BSSID”,A2)),B2&":"&C2&":"&D2&":"&E2&":"&F2&":"&G2,B2)
...
My expected results would be, in a single formula, if B2 contains the string "BSSID" that H2 would ...

How can I return a value for a cell containing data from a number of cells?

Using Excel/Numbers, I am wanting to use a formula which checks if a cell contains any of the data contained in a number of other cells, if if it does I want to return a custom value or write out another formula, if it does, I want it to retune "n/a"
Column A (The options)
DNS
Hosting
Mapping
No Appointment
Reference
Column B (The column containing the formula)
Column C (The criteria to search for)
DNS
Hosting
Mapping
No Appointment
Reference
Whilst the syntax is wrong, I'm looking to achieve this:
If A2 contains any of the data from column B then return "Cakes" if not return "N/A"
If A3 contains any of the data from column B then return "Cakes" if not return "N/A"
etc. etc.
I've tried using
=IF(LOOKUP(C2,E$2:E$5,TRUE)=TRUE,"CAKES","N/A")
The correct output was obtained on the first cell alone but the formula output errors for all other cells.
Using the following formula, I have achieved what I was wanting to do, but I welcome additional formula or any tidier ways of achieving the same goal
=IF(COUNTIF(A$2:A$50,B$2:B$5)=1,"CAKES","N/A")

Resources