How do I select just some data from a cell with multiple values? - excel

I have a CSV file imported into Excel 2010. One cell contains a portion of text that I want, but it is of different lengths and locations within the cell. Not every record has data in the cell. Each piece of text is delimited by a semicolon (;) but still within the same cell. So:
B2 = text_I_want
B3 = blank
B4 = text_I_don't_want; text_I_want_that_has_a_different_length; more_text_I_don't_want
B5 = text_I_don't_want; text_I_don't_want; text_I_don't_want; text_I_want_now;
Column M of the same row should contain the entire text I want but only what I want. So:
M2 = text_I_want
M3 = blank
M4 = text_I_want_that_has_a_different_length
M5 = text_I_want_now
There are more than 10,000 records to go through and would appreciate help.
EDIT: I didn't explain myself clearly. Column B contains a string of text which contains various hyperlinks. Each cell could contain zero hyperlinks, one hyperlink, 2, 3, 4 and so on. Each hyperlink is separated by a semicolon. I want only the amazon.com hyperlinks, not the amazon.ca or amazon.co.uk or any other hyperlinks. I hope this is a clearer example, with what I want in a separate field, bolded.
-B2 = amazon.com/12345
-B3 =amazon.ca/search?keywords=William+Shatner
-B4 =amazon.ca/12AB; amazon.com/AB1; loc.gov/fubar
-B5 =amazon.com/978037346; amazon.de/search?VX123
 
EDIT #2 - Since I can convert the text to columns, how about this questions: how do I search a row com columns A-G for any cell containing amazon.com and then copy that cell contents into column M?

You can use Find/FindB (depending on whether your content is single byte or MBCS data - the ones ending in B are for MBCS data).
In N2, for instance, you can use
=Find(M2, B2)
N2 will contain the offset (index) of the text in M2 in B2, or zero if it's not found.
To extract the text, use Mid/MidB and Len/LenB. You can add this in P2, for example:
=IF(N2 > 0, MID(B2, N2, LEN(M2)), "")
This checks to see if the value in N2 is greater than zero, and if it is copies the text from B2 starting at the position indicated in N2 for the number of characters returned as the length of the text in M2. If N2 is zero, it returns a blank space.
You could, in fact, combine both operations into one (it's a little harder to read):
=IF(Find(M2, B2) > 0, Mid(B2, Find(M2, B2), Len(M2)), "")

Based on your first edit, with amazon.com/ in B1 (for the sake of flexibility) then:
=IF(AND(IFERROR(FIND(";",B2,IFERROR(FIND($B$1,B2),"")),"")="",IFERROR(FIND($B$1,B2),"")<>""),B2,IFERROR(MID(B2,IFERROR(FIND($B$1,B2),""),IFERROR(FIND(";",B2,IFERROR(FIND($B$1,B2),"")),"")-IFERROR(FIND($B$1,B2),"")),""))
(copied down as required) should work.

Related

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 ...

If cell contains (x,y or z) allow for another cell to display its formula value

I have three columns "BF", "SF", and "LF" whose cells have theire own unique formulas. Currently, regardless of what is entered in column "Species" the BF, SF, and LF cells will compute and display their formula values.
I need help in trying to assign the formula cells to only display their formula values if the "Species" column matches a certain criteria. . .
Example:
For the BF column I would like it to display it's formulas value ONLY if the "Species" column matches any of the following. . .DF#1 GRN, DF#2 GRN, DF#1 GRN FOHC, Borate, Borate KDAT, ACQ, ACQ KDAT. . .Or possibly there is a way to say that if the cell contains DF or ACQ or Borate
For the SF Column I would like it to display it's formulas value ONLY if the "Species" column matches any of the following. . .OSB, OSB S1, CDX, CDX S1, ACX, CCX
Any help you can provide would be greatly appreciated. Thank you for your time.
You simply need an IF statement with OR logic inside. The following will display 100 if "DF#1 GRN" or "DF#2 GRN" is present in cell A2. If they are not, it will display nothing (""). You just need to put your formula in place of the 100, and add all the cases you need to the OR() separated by a comma.
=IF(OR(A2 = "DF#1 GRN", A2 = "DF#2 GRN"), 100, "")
If you want to search for substrings instead of checking if the entire cell is equal to a value, you can use ISNUMBER and SEARCH as seen below. This will check if either "DF" or "Bo" exists in cell A2 and print either 100 or nothing based on that.
=IF(OR(ISNUMBER(SEARCH("DF", A2)), ISNUMBER(SEARCH("Bo", A2))), 100, "")
So, you want another IF statement with a check for something like LEN(A1)<>LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"DF",""),"ACQ",""),"Borate",""))?

VBA Loop through rows with multiple ranges and conditions

Disclaimer: New to VBA
I have an Excel sheet where I would like to build a VBA that does the following things.
My requirement is to fill a value in row Z based on a formula determined by the text in row M.
The formula deals with summing values from row A, B, C in different combinations (determined by text in row M).
Loop this for each row starting from row 5 to row 10000.
Eg:
If cell M5 = Apple, then Z5 = A5+B5;
If cell M9 = Samsung, then Z9 = C9+A9
I know a nested IF formula does it easily but there are just too many conditions and I'm looking for an automatic and cleaner route.
Thanks!
Not a VBA, but I think this will solve the issue:
The solution requires two parts:
a) A table (or named range) to hold the string value to match, and two columns to be added. This table (BrandEq - my example) is essentially a map to indicate which cells are to be summed. Where the column (A,B,C,...) is followed by a '#" for the row to be added in later.
b) A custom formula (detailed below) in each cell (z, in your case) to return the sum of the appropriate columns.
[Please see the attached image]
Personally, I prefer tables, but either that or a named range is recommend because additional "brands" and combinations (Cell1,Cell2) (my example) can be added easily and the formula automatically updates, so no going back to the base formula.
The the "z cell" formula is:
=INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D15,BrandEq,2,FALSE),"#",TEXT(ROW(),"#####"))) +INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D15,BrandEq,3,FALSE),"#",TEXT(ROW(),"#####")))
This may be easier to understand working from the 'inside-out':
VLOOKUP (from the 'DATA' table/range- current row brand column,into the 'BRANDEQ' table/range, return the contents of the corresponding column - note 2 on the first line and 3 on the second line, FALSE=exact matches on the lookup value)
with this value (eg. a#) ->
SUBSTITUTE( the hashtag '#' place holder, with the current row() as a text() value)
Indirect( return the corresponding value of that cell -- from the data table, the formula is entered in 'zval' row 14 (my example) (eg. 'a#' -> a11 = 100)
This formula is called a second time, but this time returning the 'Cell2 value, all else is the same function. (eg. 'b#' ->b11 = 15)
The results are added together.
Row 11, for example, parses as follows:
INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D11,BrandEq,2,FALSE),"#",TEXT(ROW(),"#####")))
+
INDIRECT(SUBSTITUTE(VLOOKUP(Sheet1!$D11,BrandEq,3,FALSE),"#",TEXT(ROW(),"#####")))
INDIRECT(SUBSTITUTE(VLOOKUP('Apple',BrandEq,2,FALSE),"#",TEXT(14,"#####")))
+
INDIRECT(SUBSTITUTE(VLOOKUP('Apple',BrandEq,3,FALSE),"#",TEXT(14,"#####")))
INDIRECT(SUBSTITUTE("a#","#","11")) + INDIRECT(SUBSTITUTE("b#","#","11"))
INDIRECT("a11") + INDIRECT("b11")
100 + 15
115
If additional/different cells need to be reference they only need be included in the 'BrandEQ' Table. Note: the cells be referenced do not need to be contiguous - they can be spread throughout the row.

Excel - breaking up a cell

I have part numbers with dashs and numbers for organization purpose, and I was wondering if there's a way I could de-concatenate the string into nearby cells with a formula or two and not go through Data > Text to Columns. Any idea?
Here's a demo of a formula-based approach. The formulas will work in Excel.
https://docs.google.com/spreadsheets/d/1LXtKOsxzo1J2-D1e4e_QUqxUug191GHJ5exWSWcoS0s/edit?usp=sharing
It works like this:
A1 contains the string you want to break up, say 12424-22778-3432626-442-52523262.
B2 contains this formula: =FIND("-",$A1,1). This will find us the first occurrence of - in A1.
B3 contains this formula: =FIND("-",$A1,B1+1). This will find us the next occurrence of - in A1 (it starts looking for - from the position following what we found in B2.
B4... can be copied over from B3. If you copy the cell rather than the formula, the formula will automatically change the reference to B1 to C1 and up. In the example, it's copied over all the way to E1.
F1 contains =mid($A1,1,B1-1). This gets us the string from the first char to the B1-1th char, i.e.: start to first occurrence of -, but without the -.
G1 contains =mid($A1,B1+1,C1-B1-1). You can copy this over up to the second to last cell. The gets us the string from the previous - (+1, i.e. without the -) to the next - (-1, i.e. without the -).
J1 is the last cell and contains =mid($A1,$E1+1,len($A1)). It works the same as the previous formula, but goes up to the end of the string in A1.

Excel text formula to extract words separated by semicolons in a field

A field in Excel contains words separated by semicolons, e.g.:
A1 = save;the;national;treasure;for;good
How can I apply Excel text formulas to produce separate words from this field in another fields? E.g.:
A2 should contain a formula to get the first word ("save")
A3 should contain a (different) formula to get the second word ("the")
etc.
However these formulas should hold good even when the value in A1 changes, e.g. if the value of A1 is changed to
A1 = hello;there;how;are;you
Any help in this respect will be highly appreciated.
(The problem is writing a function of my own is not allowed in this case, I have to use original functions like find, search, mid, etc.)
You can create a VBA function to split the fields from this example:
Function ExtractElement(str, n, sepChar)
' Returns the nth element from a string,
' using a specified separator character
Dim x As Variant
x = Split(str, sepChar)
If n > 0 And n - 1 <= UBound(x) Then
ExtractElement = x(n - 1)
Else
ExtractElement = ""
End If
End Function
Then the A2 formula would be: =ExtractElement(A1, 1, ";") and A3 would be: =ExtractElement(A1, 2, ";") and so on
If you have your text to parse in A1 then the following formulas should work
In A2 enter the formula
=IF(ISERROR(LEFT(A1,FIND(";",A1)-1)),A1,LEFT(A1,FIND(";",A1)-1))
In B2 enter the formula
=IF(ISERROR(RIGHT(A1,LEN(A1)-FIND(";",A1))),"",RIGHT(A1,LEN(A1)-FIND(";",A1)))
You can then copy those down as far as you need. Column A grabs the left most word, and Column B displays the remaining string to be parsed. If it runs out of words to parse the formula will display a blank. Column B can also be hidden.
If you can use intermediate formulae, then this will work:
A1 -- save;the;national;treasure;for;good
B1 -- blank
C1 -- =IFERROR(FIND(";",$A1,1+(B1)),LEN($A1)+1)
copy C1 into D1:H1
C2 -- =MID($A1,B1+1,(C1-B1)-1)
copy C2 into D2:H2
Row 1 will display the position of each semi-colon in A1, because it starts looking in the string one character past the semi-colon found in the previous cell.
eg cell E1 searches for a semi-colon in A1 starting at D1+1 =10.
The iferror statement in C1:H1 traps the error which will occur when the search finds no further semi-colons, and returns the full length of string A1, plus 1 for an imaginary semi-colon at the end.
B1 needs to be blank to create an initial zero.
Cells C2:H2 then use the Mid function to copy the section of the A1 string starting one character after the value in each of B1:G1, with length (C1-B1)-1, (d1-c1)-1 etc (minus one to cut out the semi-colon itself)
You should get: 5, 9,18,27,31,36 in Row 1, and beneath those cells the individual words.
Hope this helps.

Resources