I have a set of names in a column "A" having "Dr.", "Prof." as prefix. I need to remove these prefixes and retain only names.
**Column A** **Required only names in column B**
Prof.Dr.med.nameX
Dr.med.nameXX
Prof. name XXX
Dr. XX
Pr.XX
Say we have a list of names in column A. First we list the unwanted junk in column C and in B1 we enter:
=TRIM(SUBSTITUTE(A1,INDEX($C$1:$C$6,SUMPRODUCT(ROW($C$1:$C$6)*ISNUMBER(SEARCH($C$1:$C$6,A1)))),""))
and copy down:
Derived from Mike H. answer
Maybe this:
=RIGHT(A1,LEN(A1)-FIND(".",A1))
Not tested!
If you have a very short and fixed-length list of substrings that you want to remove, you could do something like:
= TRIM( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( C1, $A$1, "" ), $A$2, "" ), $A$3; "" ), $A$4, "" ), $A$5, "" ) )
(with your original string in C1 and the range of substringsto be removed in A1:A5) .This is a bit messy though and not quite flexible. If you have no objections to using vba, I would propose to write a UDF similar to this:
Function UDF_MultiSubstitute(text As String, old_text_rng As Range, newText As String)
Dim curCell As Range
For Each curCell In old_text_rng
text = Replace(text, curCell.Value, newText)
Next
UDF_MultiSubstitute = Trim(text)
End Function
which you can then invoke as:
= UDF_MultiSubstitute( C1, $A$1:$A$5, "" )
Obviuosly, you might want to add some error trapping etc. to the vba code
Related
I have a formula to combine several values:
Value in column C
Add 'And' if there is a space
Drop down selection value in column I
Assigned value based on dropdown selection in column I (e.g. B1, C1)
Append B1 or C1 to value in column C, result shown in column M
The formula in column M:
=IF(I15="Truck",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and ","And"),CHAR(32),"")),IF(I15="Bus",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")& "B1"),IF(I15="Car",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")),"")& "C1"))
It works except for one tiny part; whenever the value in column C is blank, C1 is populated into column M by default. The value in column M should be blank.
Any thoughts?
The real issue is when I15 is blank. Run through with the "Evaluate Formula" tool, and you should see it, but if you break your code out and format it with indentations/new lines, etc:
=IF(I15="Truck",
CLEAN(
SUBSTITUTE(
SUBSTITUTE(TRIM(C15),"and ","And"), 'Should this be "and" rather than "and " to match the "Bus" and "Car"?
CHAR(32),
""
)
),
IF(I15="Bus",
CLEAN(
SUBSTITUTE(
SUBSTITUTE(TRIM(C15),"and","And"),
CHAR(32),
""
)& "B1"
),
IF(I15="Car",
CLEAN(
SUBSTITUTE(
SUBSTITUTE(TRIM(C15),"and","And"),
CHAR(32),
""
)
), 'Should this bracket be after the "C1" instead of here?
""
)& "C1"
)
)
Now, trace through if I15 is blank: I15 is not "Truck" and I15 is not "Bus", so we get:
=IF(FALSE,
N/A,
IF(FALSE,
N/A,
IF(I15="Car",
CLEAN(
SUBSTITUTE(
SUBSTITUTE(TRIM(C15),"and","And"),
CHAR(32),
""
)
), 'Should this bracket be after the "C1" instead of here?
""
)& "C1"
)
)
Simplify that down, and you get:
IF(I15="Car",
CLEAN(
SUBSTITUTE(
SUBSTITUTE(TRIM(C15),"and","And"),
CHAR(32),
""
)
),
""
)& "C1"
But, I15 is blank, not "Car", so it becomes IF(FALSE, N/A, "") & "C1" - meaning, it will always say "C1" if I15 is not "Bus" or "Truck".
Also, you probably can simplify your code down to =CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")) & IF(I15="Bus", "B1", IF(I15="Car", "C1", ""))
If you can set up a table somewhere, you can change the IF statement to a VLOOKUP, or if you have a table for the Drop-down values you can use CHOOSE(MATCH(..))
I have a question regarding excel.
I have two columns, column A has a number and B has country codes. I'm looking for a way to automatically go through 100's thousands of rows to group them so they look like the following...
Is this doable? I'm at a complete loss :(
THE END RESULT
6512 AG,AI,AW,BB,BL,BM,
6513 BQ,BS,BZ,CA,CR
STARTING POINT - column A & B
6512 AG
6512 AI
6512 AW
6512 BB
6512 BL
6512 BM
6513 BQ
6513 BS
6513 BZ
6513 CA
6513 CR
These solutions use a working column named Concatenated Results.
Assuming your data has a header (adjust formulas as required) and is located at B6:C34 (change as required) as in fig below.
Data sorted by ID:
Concatenated Results: Enter this formula in D7.
= CONCATENATE( C7, IF( EXACT( B7, B8 ), "," & D8, "" ) )
ID.Unique: Enter this Array Formula in E7 (FormulaArrays are entered pressing CTRL+SHIFT+ENTER simultaneously, you shall see { and } around the formula if entered correctly)
=IFERROR( INDEX( $B$7:$B$34,
MATCH( 0, COUNTIF( $E6:$E$6, $B$7:$B$34 ), 0 ) * 1 ), "" )
Countries: Enter this formula in F7
=IFERROR(VLOOKUP(E7,$B$6:$J$34,3,0),"")
Copy range D7:F7 till last row of data (i.e. row 34).
Data not sorted: If the data is not sorted enter this formula for the Concatenated Results in D7. All other formulas remain unchanged.
=CONCATENATE( C7,
IF( ISERROR( MATCH( B7, B8:B$35, 0 ) ), "",
"," & VLOOKUP( B7, B8:D$35, 3, 0 ) ) )
Suggest to read the following pages to gain a deeper understanding of the resources used:
Excel functions (alphabetical), Create an array formula, Guidelines and examples of array formulas
Try this out and let me know in case of any queries or if you require detailed explanation.
Step1: Filter the data by column A.
Step2: Make a new column C where use this formula IF(A2=A1,CONCATENATE(B2,",",C1),B2) .
Step3: Make a new column D where use this formula IF(A2=A3," ","REQUIRED_ANS") .
Step4: Filter on column D using ALT + D + F + F and you get you desired output.
My Output:
This is a VBA solution
Assuming your data, excluding header, is located at B7:C34 (change as required).
ID.Unique: Enter this Array Formula in E7 (FormulaArrays are entered pressing CTRL+SHIFT+ENTER simultaneously, you shall see { and } around the formula if entered correctly)
=IFERROR( INDEX( $B$7:$B$34,
MATCH( 0, COUNTIF( $E6:$E$6, $B$7:$B$34 ), 0 ) * 1 ), "" )
Countries: Enter this User Defined Function (UDF) in F7
=Match_Concatenated(E7,$B$7:$C$34)
Copy this UDF code in a VBA Module
Option Explicit
Public Function Match_Concatenated(vValue As Variant, rTrg As Range) As String
Dim sOutput As String
Dim vTrg As Variant, vItm As Variant, lRow As Long
vTrg = rTrg.Value2
For lRow = 1 To UBound(vTrg)
vItm = WorksheetFunction.Index(vTrg, lRow, 0)
If vItm(1) = vValue Then sOutput = sOutput & Chr(44) & vItm(2)
Next
sOutput = Replace(sOutput, Chr(44), vbNullString, 1, 1)
Match_Concatenated = sOutput
End Function
Suggest to read the following pages to gain a deeper understanding of the resources used:
Excel functions (alphabetical),
Create an array formula,
Guidelines and examples of array formulas,
Option Explicit Statement,
Range.Value2 Property (Excel), For...Next Statement,
WorksheetFunction Object (Excel), INDEX function,
If...Then...Else Statement, Replace Function
i want to get the last value after / in excel.
www.google.com/new/oldpage.php
www.google.com/new/hello/wright.html
www.facebook.com/face/newface/demo.php
from the above link i want to separate the pagename
for eg. from 1 url output should be www.google.com/new/ 2nd www.google.com/new/hello/ and so
i used RIGHT formula but we can define only static. pls. provide if any one has the solution.
If A1 is www.google.com/new/oldpage.php, use this code to get www.google.com/new/:
=LEFT( A1, FIND( "|", SUBSTITUTE( A1, "/", "|", LEN(A1)-LEN(SUBSTITUTE(A1,"/","")) ) ) )
Use this code to get oldpage.php:
=RIGHT(A1,LEN(A1)-FIND("|",SUBSTITUTE(A1,"/","|",LEN(A1)-LEN(SUBSTITUTE(A1,"/","")))))
As you can see, you need to SUBSTITUTE some chars before calling the FIND method.
Depending on your regional options, you must change "," (comma) in the formula for ";" (semicolon).
Use this formula,
=MID(A16,SEARCH("^^",SUBSTITUTE(A16,"/","^^",LEN(A16)-LEN(SUBSTITUTE(A16,"/",""))))+1,999)
Excel doesn't provide a formula for reverse string searches so the following must be used
=IF(ISERROR(FIND("/",A1)),A1,RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"/","~",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))))
I had an Excel file with some text in a column that I needed to swap out some words with a new phrase, but maintain some parts of the original text. To keep it simple for my user, I wanted to allow filtering by the original words, which meant I needed a column per word. The Excel "Text to Columns" function might have been able to help, but I didn't bother with it because I heard you needed to specify the number columns.
Long story short, here's what I came up with:
Source Cell = A2
Column with first word: B2
Column with second word: C2
A2: This is some example text
B2: =IFERROR(LEFT(A2, FIND(" ", A2)), "")
C2: =IFERROR(LEFT(SUBSTITUTE(MID($A2, FIND(B2, $A2), LEN($A2) - FIND(B2, $A2)), B2, ""), FIND(" ", SUBSTITUTE(MID($A2, FIND(B2, $A2), LEN($A2) - FIND(B2, $A2)), B2, ""))), "")
D2: filled right from C2
...
What this mess of stuff in C2 is doing is basically chopping up the source cell (A2) based on the word from the previous cell. So, B2 is looking for the first space it sees, and throwing the rest of the string away, leaving just the word "This". C2 is looking at B2, and removes the word it sees from the value of A2 and looks for the next space. D2 would look at C2, and so forth. Unfortunately, this breaks down when there is a repeated word.
If we change the example text in A2 to "This is some example text, is it a question" ("is" is repeated), it gets stuck repeating the words "is", "some", "example", "text," over and over when filled right.
I don't really need an answer for my current task, but I was hoping for a complete solution to this situation, as have not found a good solution anywhere.
Hope this is helpful for someone!
Here's a possibility using some much less ugly looking VBA:
Sub splitCell(source As Range, delimiter As String, target As Range)
'Takes value of source range and splits it by the delimiter horizontally
'starting at the target range.
Dim text() As String
text = Split(source.Value, delimiter)
For x = LBound(text) To UBound(text)
target.Offset(0, x).Value = text(x)
Next
End Sub
'Sample Call
Sub Test()
With ThisWorkbook.Sheets(1)
splitCell .Range("A2"), " ", .Range("B2")
End With
End Sub
I have names in a column. I need to split just the last names from that column into another column.
The last name is delimited by a space from the right side.
The contents in cell A2 = Alistair Stevens and I entered the formula in cell B2 (I need 'Stevens' in cell B2)
I tried using the following formulas:
=RIGHT(A2,FIND(" ",A2,1)-1)
=RIGHT(A2,FIND(" ",A2))
Both these formulas work for this cell but when I fill it down / copy and paste it for the cells below it doesn't work. I get the wrong values!!
A3 -> David Mckenzie
B3 -> Mckenzie
This works, even when there are middle names:
=MID(A2,FIND(CHAR(1),SUBSTITUTE(A2," ",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))+1,LEN(A2))
If you want everything BUT the last name, check out this answer.
If there are trailing spaces in your names, then you may want to remove them by replacing all instances of A2 by TRIM(A2) in the above formula.
Note that it is only by pure chance that your first formula =RIGHT(A2,FIND(" ",A2,1)-1) kind of works for Alistair Stevens. This is because "Alistair" and " Stevens" happen to contain the same number of characters (if you count the leading space in " Stevens").
The answer provided by #Jean provides a working but obscure solution (although it doesn't handle trailing spaces)
As an alternative consider a vba user defined function (UDF)
Function RightWord(r As Range) As Variant
Dim s As String
s = Trim(r.Value)
RightWord = Mid(s, InStrRev(s, " ") + 1)
End Function
Use in sheet as
=RightWord(A2)
Try this function in Excel:
Public Shared Function SPLITTEXT(Text As String, SplitAt As String, ReturnZeroBasedIndex As Integer) As String
Dim s() As String = Split(Text, SplitAt)
If ReturnZeroBasedIndex <= s.Count - 1 Then
Return s(ReturnZeroBasedIndex)
Else
Return ""
End If
End Function
You use it like this:
First Name (A1) | Last Name (A2)
Value in cell A1 = Michael Zomparelli
I want the last name in column A2.
=SPLITTEXT(A1, " ", 1)
The last param is the zero-based index you want to return. So if you split on the space char then index 0 = Michael and index 1 = Zomparelli
The above function is a .Net function, but can easily be converted to VBA.
If you want to get the second to last word in a text, you can use this macro as a function in your spreadsheet:
Public Function Get2ndText(S As String) As String
Dim sArr() As String
Dim i As Integer
sArr = Split(S, " ")
'get the next to the last string
i = UBound(sArr) - 1
Get2ndText = sArr(i)
End Function
Then in your spreadsheet B1 as the text:
CURRENT OWNER 915 BROADWAY ST HOUSTON TX 77012-2126
in B2 your formula would be:
=Get2ndText(B1)
The result would be
TX
Simpler would be:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",99)),99))
You can use A2 in place of TRIM(A2) if you are sure that your data doesn't contain any unwanted spaces.
Based on concept explained by Rick Rothstein:
http://www.excelfox.com/forum/showthread.php/333-Get-Field-from-Delimited-Text-String
Sorry for being necroposter!
Right(A1, Len(A1)-Find("(asterisk)",Substitute(A1, "(space)","(asterisk)",Len(A1)-Len(Substitute(A1,"(space)", "(no space)")))))
Try this. Hope it works.
Try this:
=RIGHT(TRIM(A2),LEN(TRIM(A2))-FIND(" ",TRIM(A2)))
I was able to copy/paste the formula and it worked fine.
Here is a list of Excel text functions (which worked in May 2011, and but is subject to being broken the next time Microsoft changes their website). :-(
You can use a multiple-stage-nested IF() functions to handle middle names or initials, titles, etc. if you expect them. Excel formulas do not support looping, so there are some limits to what you can do.
RIGHT return whatever number of characters in the second parameter from the right of the first parameter. So, you want the total length of your column A - subtract the index. which is therefore:
=RIGHT(A2, LEN(A2)-FIND(" ", A2, 1))
And you should consider using TRIM(A2) everywhere it appears...
Try this:
Right(RC[-1],Len(RC[-1])-InStrRev(RC[-1]," "))