asked on
Capture first letters of string of words in Excel or Word
Hello,
Is there a way in Excel or Word (preferably Excel) to extract only the first letters from a string of words?
For example, suppose cell A1 contains the following text:
"Four score and seven years ago, our fathers brought forth on this continent a new nation conceived in liberty and dedicated to the proposition that all men are created equal."
What formula would capture a string of the first letters of each word thus:
Fsasya,ofbfotcannciladttptamace.
One method that comes to mind is to paste the content into a single cell then use the Text to Column tool with the delimiter set to "Space" so that the text is spread out over multiple columns with each column containing a single word. Then, in row B, use the formula
=LEFT(A1,1)
This formula displays the first letter of each word. The trick now is to concatenate the string of first letters into a single cell. Doing so requires a formula such as =Concatenate(B1,B2,B3. . .) or = (B1&B2&B3. . .)
Note: even though the =Concatenate() formula is built-in to Excel, it still seems to require just as many steps (ie clicking each cell followed by a comma) as the formula using "&". I tried using a colon (eg B1:B60) to represent the long range in the =Concatenate() formula but it doesn't seem to work.
I'm wondering if there is a simpler way to capture a string of first letters.
Thanks
Right-click on your workbook name in the "Project-VBAProject" pane. If you don’t see an existing module then select Insert -> Module from the menu bar. Otherwise just select the module.
Copy the UDF (you can use the ‘Select All’ button if you like) and paste it into the right-hand pane of the VBA editor "Module1" window
Press Alt+F11 again to go back to Excel
When you close the workbook you will need to save it as an xlsm, xlsb or xls file if it’s not already one of those.
The name of my function can be renamed anything you like by changing all 3 occurrences of the name.
ASKER
Thanks for the comments and sample workbook. Unfortunately it gave me a #NAME? error in H2:I4. I suspect that is due to me still using Excel 2010. I have continued to be reluctant to give up the option of having multiple worksheets share a common toolbar in a single workbook.
Thanks
If you must avoid VBA, you can use Text to Columns to split your source text into individual words like you had suggested in your original question body. And then in a row underneath, copy across a formula in cell B3 (assuming that contains your first word) that produces a growing concatenation, The last cell with this formula carries your result.
=A3 & LEFT(B2,1)
Brad
ASKER
Of course! LOL
In case you don't recognize the origin of that phrase, it's from the movie, O Brother Where Art Thou. I think it's the funniest line I've ever heard in a movie. :)
=TEXTJOIN("", TRUE,LEFT(TRANSPOSE(FILTER
Function Q_29228656(r As Range, Optional letter_count As Long = 1) As String
Static oRE As Object
Static lngPriorCount As Long
Dim oMatches As Object
Dim oM As Object
If (oRE is Nothing) Or (letter_count <> lngPriorCount) Then
Set oRE = CreateObject("vbscript.regexp")
oRE.Global = True
oRE.Pattern = "(?:^|\W)(\w{" & letter_count & "})"
lngPriorCount = letter_count
End If
Set oMatches = oRE.Execute(r.value)
For Each oM in oMatches
Q_29228656 = Q_29228656 & om.Submatches(0)
Next
End Function
Using the RegExpReplace function from that article, you could use a formula like this:
=RegExpReplace(A1,"( *)(\w)(\w*)","$2",TRUE,FALSE)
In English, that pattern string means:
- A space, zero or more times, followed by
- A single "word character" (letter, numeral, or underscore), followed by
- Zero or more word characters
The $2 indicates that for each match to the whole pattern string, replace it with the second submatch, which is the single word character (and the first character in each word).
The UDF code:
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
Optional MultiLine As Boolean = False)
' Function written by Patrick G. Matthews. You may use and distribute this code freely,
' as long as you properly credit and attribute authorship and the URL of where you
' found the code
' This function relies on the VBScript version of Regular Expressions, and thus some of
' the functionality available in Perl and/or .Net may not be available. The full extent
' of what functionality will be available on any given computer is based on which version
' of the VBScript runtime is installed on that computer
' This function uses Regular Expressions to parse a string, and replace parts of the string
' matching the specified pattern with another string. The optional argument ReplaceAll
' controls whether all instances of the matched string are replaced (True) or just the first
' instance (False)
' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
' instead
' By default, RegExp is case-sensitive in pattern-matching. To keep this, omit MatchCase or
' set it to True
' If you use this function from Excel, you may substitute range references for all the arguments
' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
' where a large number of calls to this function are made, making RegX a static variable that
' preserves its state in between calls significantly improves performance
Static RegX As Object
If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = PatternStr
.Global = ReplaceAll
.IgnoreCase = Not MatchCase
.MultiLine = MultiLine
End With
RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
End Function
ASKER
ASKER
If not, can you tell me where to paste the code and in what type of VBA screen? Also, will this work for multiple rows?
I'm sorry, I do not know what WTYWAT means. It brings to mind WYSIWYG. Is it similar?
Thanks