Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Return middle part of a string

Posted on 2011-09-07
Last Modified: 2012-08-14
I have a string that looks like the following:


I am looking for a vba function that would be most efficient to return the "TheWordIWant" string.

I am wanting this to look like the following:

Function GetTabName(strFileName as String, strDelimiter as String, intDelimiterOccurance as Integer) as String

End Function
Question by:ckelsoe
LVL 29

Expert Comment

by:Randy Downs
ID: 36497044
Try this


Extract nth Word From Text in Excel See
 Also: Extracting Words From Text in Excel using Excel Built in Function/Formulas and Find Nth Occurrence

With the aid of Excel VBA we can write a custom formula/function, or user defined function to extract out the nth word from a text string. The code below should be placed in a standard Excel Module after entering the VBE. That is, push Alt+F11 and then go to Insert>Module and paste in the code below;

Option Compare Text

Function Get_Word(text_string As String, nth_word) As String

Dim lWordCount As Long

        With Application.WorksheetFunction

        lWordCount = Len(text_string) - Len(.Substitute(text_string, " ", "")) + 1


        If IsNumeric(nth_word) Then

           nth_word = nth_word - 1

            Get_Word = Mid(Mid(Mid(.Substitute(text_string, " ", "^", nth_word), 1, 256), _
                .Find("^", .Substitute(text_string, " ", "^", nth_word)), 256), 2, _
                .Find(" ", Mid(Mid(.Substitute(text_string, " ", "^", nth_word), 1, 256), _
                .Find("^", .Substitute(text_string, " ", "^", nth_word)), 256)) - 2)

        ElseIf nth_word = "First" Then

            Get_Word = Left(text_string, .Find(" ", text_string) - 1)

        ElseIf nth_word = "Last" Then

            Get_Word = Mid(.Substitute(text_string, " ", "^", Len(text_string) - _
            Len(.Substitute(text_string, " ", ""))), .Find("^", .Substitute(text_string, " ", "^", _
            Len(text_string) - Len(.Substitute(text_string, " ", "")))) + 1, 256)

        End If

    End With


End Function
LVL 39

Accepted Solution

nutsch earned 500 total points
ID: 36497055
here it should be:

Function gettabname(strFileName As String, strDelimiter As String, intDelimiterOccurance As Integer) As String
Dim arr As Variant

arr = Split(strFileName, strDelimiter)

sWordIwant = arr(intDelimiterOccurance - 1)

End Function

Open in new window

LVL 31

Expert Comment

ID: 36497058
You can use the Split function to do this (I did it in Access, but I think it would work in Excel VBA too):
Public Function ThirdName(strFullString As String) As String
   Dim strFullText() As String
   Dim intUBound As Integer
   'Extract third name from full name
   strFullText = Split(strFullString, "_", -1, vbTextCompare)
   intUBound = UBound(strFullText)
   ThirdName = strFullText(2)

End Function

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 31

Expert Comment

ID: 36497065
The intUBound line is redundant (I modified this from another function that used it)
LVL 39

Expert Comment

ID: 36497072
fixing error on line 6

Function gettabname(strFileName As String, strDelimiter As String, intDelimiterOccurance As Integer) As String
Dim arr As Variant

arr = Split(strFileName, strDelimiter)

gettabname = arr(intDelimiterOccurance - 1)

End Function

Open in new window


Author Closing Comment

ID: 36497403
I am accepting this solution over others as it is simple with the least amount of coding to achieve the intended result. This is not to say that other code presented would not work. I did note the correction in Line 6 and had made the same correction to meet my specific goals.

Thanks for the quick response.
LVL 39

Expert Comment

ID: 36497427
Glad to help. Thanks for the grade.


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question