Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sorting/Replacing by Internal Character

I have a column of scientific names that look like this:


canis lupus

argeo (haguel) savin

plumeria algers


I would like to delete all the parentheses and their contents, so the finished product will look like this:


canis lupus

argeo savin

plumeria algers


However, I would first like to sort the column so that all the scientific names that includes parentheses are grouped together. I can then create a new column and make a check mark to indicate values that previously contained parentheses.


So I just wondered if there's a way to 1) sort columns according to special characters the various values contain, and 2) delete all parentheses and everything they contain. And I should add that the parentheses don't all enclose "haguel." There are many different words enclosed in parentheses.


Thanks.

MacBook Pro

Posted on Apr 28, 2016 6:47 PM

Reply
Question marked as Best reply

Posted on Apr 28, 2016 9:13 PM

you can do this without sorting by following this example:

User uploaded file

In column A is the original names


B1=IFERROR(FIND("(", A1), 0)

this is shorthand for... select cell B1 ,then type (or copy and paste from here) the formula:

=IFERROR(FIND("(", A1), 0)


C1=IFERROR(FIND(")", A1,start-pos), 0)

D1=IF(AND(B1>0, C1>0), LEFT(A1, B1−1)&RIGHT(A1,LEN(A1)−C1), A1)


select cells B1 thru D1, copy

select cells B1 thru the end of column D, paste


now column D contains the "corrected" names. select column D copy


now select the destination and use the menu item "Edit > Paste Formula Results"

4 replies
Question marked as Best reply

Apr 28, 2016 9:13 PM in response to David Blomstrom

you can do this without sorting by following this example:

User uploaded file

In column A is the original names


B1=IFERROR(FIND("(", A1), 0)

this is shorthand for... select cell B1 ,then type (or copy and paste from here) the formula:

=IFERROR(FIND("(", A1), 0)


C1=IFERROR(FIND(")", A1,start-pos), 0)

D1=IF(AND(B1>0, C1>0), LEFT(A1, B1−1)&RIGHT(A1,LEN(A1)−C1), A1)


select cells B1 thru D1, copy

select cells B1 thru the end of column D, paste


now column D contains the "corrected" names. select column D copy


now select the destination and use the menu item "Edit > Paste Formula Results"

Apr 28, 2016 9:13 PM in response to David Blomstrom

David Blomstrom wrote:


1) sort columns according to special characters the various values contain, and 2) delete all parentheses and everything they contain



Taking 2) first, here is one way, using a single formula:


User uploaded file


In cell B2, filled down:


=IFERROR(SUBSTITUTE($A2,MID($A2,FIND("(",$A2,1),FIND(")",$A2,1)−FIND("(",$A2,1) +2),""),$A2)


For 1) to mark the entries that originally included ( ) for sorting if you want, the formula in C2, filled down:


=A2<>B2


SG

Apr 28, 2016 8:29 PM in response to David Blomstrom

Hi David,


You can't sort on "contains (", but you can filter the table on that column with the rule 'text' 'contains' '(' to show only the rows containing parentheses, then mark all the visible rows with a √ in your added column.


OR you can do the marking automatically, using the formula in the added column (B).

AND you can do the removal automatically using the formula in column C. (see notes below)

User uploaded file

Formulas (entered in row 2 and filled down to the end of the column):


B2: =IF(ISERROR(FIND(")",A,start-pos)),"","√")


Find returns a number if the closing parentesis ( ")" ) is found, and an error is it is not.

ISERROR returns TRUE if there is an error, FALSE if there is not.

IF returns a null string ( "" ) on TRUE, a check mark ( "√" ) on FALSE. You can get a better checkmark from the character viewer—Show emojis and symbols in the input menu, which displays as a flag indicating the keyboard layout you are using.


C2: =IFERROR(REPLACE(A,FIND(" (",A),FIND(")",A)+1−FIND(" (",A),""),A)


FIND(" (",A) returns the position of the space before the opening parenthesis (4 in A3), used by REPLACE as the position to start when replacing the string.

FIND(")",A)+1 returns one more than the position of the closing parenthesis (10 in A3), and

-FIND(" (",A) repeats the first find, and subtracts the result (4) from the result of the second find (10-4); REPLACE uses the result to tell how many characters to replace.

"" tells REPLACE what (a null string) to insert as the replacement for the selected characters.


The first FIND will throw an error if it cannot find " (" in the string in A. That is caught by IFERROR, which skips the rest of the calculation and goes to the last argument, ( A) which tells it to place the contents of the cell in this row of A into the cell containing the formula.


CAUTION: Formulas are 'live.' Their results change when the data in cells they reference changes. To preserve the values in columns B and C you must, before editing column A:

Select all of the cells in columns B and C, and Copy.

With the cells still selected, go Edit (menu) > Paste Formula Results.

This replaces the formulas in these columns with the results last calculated.


You may then want to Copy the cells in column C and Paste into column A, then delete column C, leaving the shortened names in A, and the checkmarks in B.


Regards,

Barry

Apr 29, 2016 7:20 AM in response to David Blomstrom

Barry's use of REPLACE is more succinct than SUBSTITUTE.


=IFERROR(REPLACE(A,FIND(" (",A),FIND(")",A)+1−FIND(" (",A),""),A)


vs.


=IFERROR(SUBSTITUTE(A,MID(A,FIND("(",A,1),FIND(")",A,1)−FIND("(",A,1)+2),""),A)


and the explanation (which in essence applies to both formulas in that you're replacing part of the string with "") is great.


SG

Sorting/Replacing by Internal Character

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.