I am trying to:
extract clients' middle name(s) from a field; or
delete the first and last name, leaving the middle name(s)
but I am stuck. I can remove the last word using
=LEFT(TRIM(A1),FIND("~",SUBSTITUTE(A1," ","~",LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))))-1)
I can remove the first word by
=RIGHT(A1,LEN(A1)-FIND(" ",A1))
But I can't combine them. I have looked online and found this:
=TRIM(REPLACE(SUBSTITUTE((TRIM(REPLACE(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),0*LEN(A1)+1,LEN(A1),"")))," ",REPT(" ",LEN(A1))),(1+LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-1)*LEN(A1)+1,LEN(A1),""))
But it doesn't work.
Just put the ' remove the last word ' formula and use it in the ' remove the first word ' formula .
Implementation : Just replace all A1 in the ' remove the first word ' formula with ' remove the last word ' formula .
=RIGHT(LEFT(TRIM(A1),FIND("~",SUBSTITUTE(A1," ","~",LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))))-1),LEN(LEFT(TRIM(A1),FIND("~",SUBSTITUTE(A1," ","~",LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))))-1))-FIND(" ",LEFT(TRIM(A1),FIND("~",SUBSTITUTE(A1," ","~",LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))))-1)))
Done.
Hope it helps.
Related
I found a lot of formulas on the Internet which copies "n characters" but they did not provide a solution for me. I am searching for a VBA code which is able to move the specific value from a column to the beginning of adjacent column. For example, the VBA code will check the A column for the word "Warm" and "Cold", and if they are available, it will move them as shown in the example. Thank you in advance for your help.
Example
Edit ( Solution by #Dominique ): =IF(A1<>"";IF(RIGHT(A1;4)="Warm";"Warm "&B1;B1))
You can't move a part of one cell to another. This is what you need to do:
Search for " Warm" (or " Cold") in a cell in column A and replace " Warm" (or " Cold") by an empty string within that cell.
Take the cell, next to that (use the Offset() function) and add the word "Warm " (or "Cold ") at the beginning of that cell.
Do this for the entire A column.
I have value as below into columnA:
00259695 YYYYMMDD.CSV
004001 YYYYMMDD.CSV
004002 YYYYMMDD.CSV
0051TZTT YYYYMMDD.CSV
04002B YYYYMMDD.CSV
How to simply add "-" instaed of blank space between name and YYYYMMDD.csv?
To get like this:
00259695-YYYYMMDD.CSV
004001-YYYYMMDD.CSV
004002-YYYYMMDD.CSV
0051TZTT-YYYYMMDD.CSV
04002B-YYYYMMDD.CSV
With data in column A, in B1 enter:
=SUBSTITUTE(A1," ","-")
and copy downwards:
Select the column where there's your date format
CTRL+H
Find : add the blank space
Replace with : just the '-'
And then replace all
You could easily find it with Google
Use excels find and replace,
Copy the white space only,
Select only the range you wish to do this to.
CTRL+h
Expand on the options and un-tick match all cell
Enter in the replace section "-"
Done
Did you want a macro?
I have a CSV in which column A is populated with strings, such as:
ABCDE/FGHI/JKL/MNOPQR
I need to populate column C with everything after the last occurrence of the "/". In this example, it would have to be "MNOPQR".
Is there a function that could be used for this? "RIGHT" doesn't seem to do the trick. I don't know what the length of the substring will be in each row, so I definitely have to look for the "/".
If your text is in A4, put this in another cell:
=MID(A4,LEN(LEFT(A4,FIND(CHAR(1),SUBSTITUTE(A4,"/",CHAR(1),LEN(A4)-LEN(SUBSTITUTE(A4,"/",""))))))+1,LEN(A4)-LEN(LEFT(A4,FIND(CHAR(1),SUBSTITUTE(A4,"/",CHAR(1),LEN(A4)-LEN(SUBSTITUTE(A4,"/",""))))))+1)
I think that should work. Thanks to #Jerry for the main part, where it finds the last / in a string.
edit:
Per #ScottCraner, this is shorter: =MID(A1,SEARCH("}}}",SUBSTITUTE(A1,"/","}}}",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))+1,LEN(A1))
Here's a bit shorter formula to return the last delimited substring in a string.
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))
Replace the delimiter with 99 spaces, then return the rightmost 99 characters. The leading characters must be spaces also, so TRIM gets rid of them.
A simple formula approach using FilterXML might be:
=FILTERXML("<items><i>" & SUBSTITUTE(A1,"/","</i><i>") & "</i></items>","//i[position()=last()]")
Profitting from the dynamic features of vers. 2019+ you can change the cell address to a range input, e.g. A1:A10 allowing output in a so called spill range.
VBA approach
As the VBA tag has been recently added to OP,
an obvious VBA approach would be to split the string input and get the last array element via Ubound():
Dim tmp As Variant
tmp = Split("ABCDE/FGHI/JKL/MNOPQR", "/")
Debug.print tmp(Ubound(tmp)) ' ~~> MNOPQR
I work in sourcing and I'm trying to look through a list of cells that contain part numbers and their description, but only pull the part number from the cell and place it in another column.
The part numbers have one of 10 possible prefixes (i.e. ABC, DDA, GHF, AH, etc.). The cells may look something like this:
Tire Pressure ABC123873 Monitor
Oil Life ABC849999999021 gauge
Air conditioner GHF211 maintenance
And I want to be able to search that entire list and only pull the following information into another column:
ABC123873
ABC849999999021
GHF211
As you can see from above, the challenge is that the part numbers are all different lengths and have no particular convention to them. The only thing you know about them is that they can have one of ten possible prefixes as I mentioned above (ABC, GHF, etc.).
The current solution I have looks something like this:
C2=FIND("ABC", A2)
D2=FIND(" ", A2, C2)
E2=MID(A2, C2, D2)
Where cell A2 contains the complete part number and description, C2 finds the beginning location of the part number by searching for its prefix, D2 finds the ending location of the part number by searching for a space, and then E2 pulls the substring between those two locations.
As you can see, this isn't a very robust solution and it only allows me to search for parts that start with ABC. I want to be able to search for all 10 possible prefixes (ABC, DDA, GHF, AH, etc.) but that does not work. I tried the approach below:
C2=FIND({"ABC", "DDA", "GHF", "AH"}, A2)
But that only searches for the ABC parts and disregards the other prefixes. Any help you all can offer will be greatly appreciated!!
This is how I would do it. I created a list of prefixes to search in column G. You could even put them on a separate page to keep it cleaner.
=MID(A1,FIND(G1:G2,A1,1),FIND(" ", A1, FIND(G1:G2, A1, 1))-FIND(G1:G2,A1,1))
I only tested two prefixes but just change G1:G2 to G1:G10 and put all you prefixes in them. The formula looks in A1. The first FIND finds the beginning character number. The rest of the equation is calculating the end of the product number minus the beginning which returns the amount of characters to return. Its important to know that MID isn't wanting the beginning and end character. It wants the beginning character and the number of characters after that to return. So FIND(" ", FIND(G1:G2, A1, 1)) does not work as it returns too many characters. Give it a try.
Oh and don't forget to do Ctrl+Shift+Enter to enter the array formula
The VBA answer would be
Sub findProductCode()
' Variable Declarations
Dim productName As String
Dim productArray() As String
Dim wordCount As Integer
' Sets the active sheet to use
' Change Sheet1 to the name of your sheet
With Sheets("Sheet1")
' Selects the first cell to check
' Change A1 to the first cell with data
Range("A1").Select
' Loops through all rows until an empty row
' It will end if you have any empty rows in the midle of data
Do Until IsEmpty(ActiveCell)
' Reads text out of the cell
'Change A to the proper column
productName = Range("A" & ActiveCell.Row).Text
' Splits the string into individual words
productArray = Split(productName, " ")
' Loops through array to find the product number
For wordCount = LBound(productArray) To UBound(productArray)
' Check if the word has the desired 10 prefixes
' Add more Or Left(productArray(wordCount), 3) = "xxx" until you have 10 of your prefixes you need
If Left(productArray(wordCount), 3) = "ABC" Or Left(productArray(wordCount), 3) = "GHF" Then
' Sends the product number to its cell
' Change B to the correct destination Cell
Range("B" & ActiveCell.Row).Value = productArray(wordCount)
End If
Next
' Increments the active cell to the next cell down
ActiveCell.Offset(1, 0).Select
Loop
End With
End Sub
If you don't know how, you need to enable the developer tab in excel. File->Options->Customize Ribbon Add the Developer to the Ribbon. The save your worksheet as .xlsm It is the macro enabled workbook. Then go to the developer tab, Choose "Visual Basic" and double click "ThisWorkBook". Paste the code in. You can run the code from there for a one time deal or you can create a button on the sheet that you can run this macro whenever you want.
To add a button, go back to the sheet you want the button on. On the developer tab select Insert and choose the first form control Button. Draw the button on your sheet and a dialog box will appear. select the findProductCode macro and select OK. Now every time you click the button it will run the macro.
This isn't the prettiest formula, but it should get you on your way. There's definitely a VBA solution, if you're interested in a macro instead of a formula.
Assuming your Tire Pressure ABC123873 Monitor is in A1, enter this into B1:
=MID(SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1),SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)),SEARCH(" ",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1))-SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)))
How it works, is it takes your A1, and first uses Substitute. I substitute the first and second [space] for ; and -. (Note, if there are more spaces than two, this will run into issues). Then, I just use Mid to extract the part of the cell starting with ABC and going until it hits the space. If you don't have "ABC" starting the values you want, you need to adjust the Search("ABC"... part (or use an array there). I am still working on it, but hopefully this helps give an idea.
I've a huge data in excel file.
For eg: say i've a word like paul son,i've to make it as paulson.
input:paul son
output:paulson.
In some cells ,i've data like mic-li,when this type of words come,it should not replace any thing,it should only remove spaces in between words.
Suppose the data is in the B column, write in the C column the formula:
=SUBSTITUTE(B1," ","")
Copy&Paste the formula in the whole C column.
edit: using commas or semicolons as parameters separator depends on your regional settings (I have to use the semicolons). This is weird I think. Thanks to #tocallaghan and #pablete for pointing this out.
It is SUBSTITUTE(B1," ",""), not REPLACE(xx;xx;xx).
Steps (1) Just Select your range, rows or column or array ,
(2) Press ctrl+H , (3 a) then in the find type a space
(3 b) in the replace do not enter anything,
(4)then just click on replace all.....
you are done.
Just wanted to add on to vulkanino's answer... now 10 years later ha. This is what I was looking for maybe someone else is too. You can also build on multiple =SUBSTITUTE() functions.
Here is what I used to format a list of names for Active Directory accounts. B2 is where the name would go. I wanted to change the space between the First name and last to a period and omit any hyphens from names.
=SUBSTITUTE(SUBSTITUTE(B2, " ", "."), "-", "")
For example the line above would look like this:
First Last-Name => First.LastName
, "-", "")
This part removes the hyphen.
(B2, " ", ".")
This part selects the cell to modify and changes empty space to a period.
=SUBSTITUTE(SUBSTITUTE(...)...)
I used two substitute functions one inside the other.