Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Programmatically Find the Nth Word in a Text String


View products that this article applies to.

Summary

In Microsoft Excel, you can create a Microsoft Visual Basic for Applications function that can extract a specific word from a text string. This article describes how to create and use a custom function to find a specific word in a text string.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS To create and use a custom function to find a specific word in a text string, follow these steps:
  1. Start Excel.
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. Type the following code in the module sheet:
    Function FindNthWord(WordStrg As Variant, occur)
        ' Declare variables where WordStrg is the string whose Nth word you
        ' want to extract and occur is the number of the word (or Nth word)
        ' you want to extract from the string.
        Dim y, z As Integer
        Dim WordSearch()
         y = (Len(WordStrg) - (Len(Application.Substitute(WordStrg, " ", _
            "")))) / 1
        ReDim WordSearch(1 To (y + 1))
        z = 1
        For SearchLoop = 1 To y
            z = (InStr(z, WordStrg, " "))
            WordSearch(SearchLoop) = Left(WordStrg, z)
            WordStrg = Application.Trim(Mid(WordStrg, z + 1))
            z = 1
        Next SearchLoop
        WordSearch(y + 1) = WordStrg
        ' Assigns the result to the function.
        FindNthWord = WordSearch(occur)
    End Function
    					
  5. Press ALT+F11 to return to Excel.
  6. Type the following text in a worksheet:
       A1: For a comparison of Dow Jones
       A2: Industrial Averages and the price of this
       A3: stock over the same quarter, refer to the
       A4: next section of the worksheet.
    					
  7. Type the following formula in cell B1 to find the second word in cell A1:
    =FindNthWord(A1,2)
  8. With cell B1 selected, grab the fill handle and fill the formula down through cell B4.

    The resulting worksheet looks like the following:
       A1: For a comparison of Dow Jones               B1: a
       A2: Industrial Averages and the price of this   B2: Averages
       A3: stock over the same quarter, refer to the   B3: over
       A4: next section of the worksheet.              B4: section
    					

↑ Back to the top


References

For more information about the InStr function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type instr function in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213268, kbprogramming, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213268
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 272