|
Extract the Last Word from a Cell in Excel - User Defined Delimiter Text Extraction - UDF
This UDF (user defined function) extracts the last word or characters from a cell in Excel. This is done by finding the last separator (i.e. a space) before a set of words or characters in Excel and then returning what comes after that last separator. This means that you can return characters, part numbers, etc. that aren't whole words as long as there is a space, dash, comma, period, etc. before what you want to extract from the cell. The general explanation for this is that you can return the last word from a cell in Excel
This is a very easy function to use and has only two arguments, text and separator. The text argument is either actual text or a cell reference. The separator argument is an optional argument and refers to what separates the words. If you leave this argument blank, the default setting is a space.
Where to install the macro: Module
UDF to Extract the Last Word from a Cell in Excel - User Defined Delimiter Text Extraction
Function GETLASTWORD(Text As String, Optional Separator As Variant)
Dim lastword As String
If IsMissing(Separator) Then
Separator = " "
End If
lastword = StrReverse(Text)
lastword = Left(lastword, InStr(1, lastword, Separator, vbTextCompare))
GETLASTWORD = StrReverse(Replace(lastword, Separator, ""))
End Function
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
I'm looking for something like the code below, except the final result contains only numbers and is devoid of cell references:
VB:
Function CellFormula(Rng As Range) As String
CellFormula = Rng.Formula
End Function
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
Ideally, if a referenced cell, itself, contains cell references, it would pull those raw numbers along with their algebraic relationship--and so on, and so on, until all cell references and their mathematical relationships are encompassed into a single formula with no cell references.
Does anyone have the solution? Your help is greatly appreciated.
I know this can be done using text to columns, however, my situation requires that I do this with a formula in a cell next to my text string (sorry, no VB code).
Constants:
1- There are always 6 values separated by 5 semi-colons
2- None of the "good" data will have semi-colons (the only time a semi-colon is found in the text string because it's my delimiter
3- The "good" data between the semi-colons can vary in length
Here are 2 samples of the data:
123.1.135.2;255.255.0.0;yahoo.com;ABC v1.2.3;16A15B14;Status=Good
222.10.246.30;255.255.0.0;cnn.com;DEF v2.30.40;17C2D52;Status=Failed
For my formula, I want to extract out the 4th value (which is "ABC v1.2.3" for record 1 and "DEF v2.30.40" for record 2.
Thanks in advance!
Hello everyone,
Is it possible to extract a certain word or number from a text file and place it in an excel sheet?
Example:
I want to extract the following numbers that appear after {"planet_id":" and have just the numbers extracted and be place in an excel sheet or word document.
var mapData = JSON.decode('{"325619":{"debris":{"32561906":{"planet_id":"32561906","r1":"14331","r2":"19008"}},"pl anets":{"32561902":{"planet_id":"32561902","player_name":null,"alliance_name":null,"alliance_id":
null,"user_id":null,"system_id":"325619",
Thanks in advance,
Elegidos (Spain)
I want to format a cell that will take user defined numbers and text in the
following format: 12345678-A
Another example would be: 23454368-X
The numbers, dash, and text refer to Medicaid identification numbers and I
want to be able to have a constant format when these values are entered.
Respected sir,
I ran with my legs - extract "with"
I have big cat - extract "big"
both words are extracted after two spaces
i want to have my house - Extract "have"
i want to dance in my house - Extract "dance"
Above both words are extract after three spaces
how do i do above
regards
juzar para
Hi,
I am creating a Excel template file using VBA Macro code. I need to create a list box with my own data to all rows of selected colum. Please give me the source for adding the combo list box to all rows of selected column in excel file.
Note: The list values should be hard coded in the source.
Thanks & Regards,
S. Sakthivel
Hi all,
I am a novice in Programming,
I need a help on the following task
I have data in the word file in form of tabular column... there are about 100 word files.. the data from these file are to be consolidated into a single excel sheet..
Please help me with the code to extract the content..
Thanks in advance
Sathish
Hi,
Sorry if the question is a bit simple but I got stuck with this problem:
I can't seem to make user-defined format that puts a text in front of a number and/or a text.
Let's say I have A1: 13, A2: texttext A3: text7 and I want to format a lot of cells to "Ilike 13" / "Ilike texttext" / "Ilike text7"... ie add the same text in the front of the cell, no matter what the content is.
I did manage it seperately, with "texttext" @ for text and "texttext" # for numbers, but what's the general one ?
Sorry again if too simple, just can't figure it out.
Hi, a newuser here.
I found this forum using a search for a way to split text in vba code and use the result. I found the very thing I needed, but have not been able to use the result in VBA without first making a cell in excel equal the result. Probably something simple but can't seem to crack it.
The task I'm doing is creating a list of goalscorers and the time they scored in cells of their own from a list thats made up of strings.
A row in this list could contain just a single name and one goal time, so would not need any action.
however it may contain several names and goal times and the problem part several goal times against a name.
The function I'm using TextSplit1 = Split(Timescored, " ")
Example list
May (46)
Hutchinson (61) (75)
Wright (27) Hayles (90)
By adding a "]" to all the ")" and then using the text to columns wizard and splitting on "]" it will split each goal into a new column. However where 2 or more goals are scored by the same player it will just leave the time of the goal scored.
So as we only need the name from the cell to the left I've written the following VBA code (leaving off the bits to move a cell down and repeat....
VB:
Sub Strings2()
Dim Timescored
Dim Playername
Dim Southend
Dim TextSplit1
L = Len(ActiveCell)
Playername = ActiveCell.Text
Timescored = ActiveCell(1, -1).Text
[COLOR=red][I] 'Msgboxes used just to show value in test runs[/I][/COLOR]
MsgBox Playername
MsgBox Timescored
If L < 4 And L > 0 Then
TextSplit1 = Split(Timescored, " ")
Cells(5, 10) = TextSplit1
Southend = Cells(5, 10)
[COLOR=red][I] 'Southend = TextSplit1 - Doesn't work[/I][/COLOR]
RevisedPlayername = Southend & " " & Playername & ")"
ActiveCell = RevisedPlayername
End If
End Sub
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
I can't get the value of TextSplit1 to be passed to Southend so have to make cell (5,10) = TextSplit1 and then Southend = that cell! Obviously not good practice and slows the macro down, when it may need to do this 30 or so times. I've tried to Dim things as Strings etc, but to no avail.
Hi
Let us say I have a cell with this information
+12.5 (-110)
How can I get rid of the (-110) on the cell and come up with a new cell that say only
+12.5
Thanks
Ricky
|
|