Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return middle part of a string

Posted on 2011-09-07
7
Medium Priority
?
211 Views
Last Modified: 2012-08-14
I have a string that looks like the following:

firstword_secondword_thirdword_TheWordIWant_fifthword_sixthword_seventhword.csv

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
0
Comment
Question by:ckelsoe
[X]
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
7 Comments
 
LVL 30

Expert Comment

by:Randy Downs
ID: 36497044
Try this

http://www.ozgrid.com/VBA/extract-words-function.htm

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
0
 
LVL 39

Accepted Solution

by:
nutsch earned 2000 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

0
 
LVL 31

Expert Comment

by:Helen Feddema
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Expert Comment

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

Expert Comment

by:nutsch
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

0
 

Author Closing Comment

by:ckelsoe
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.
0
 
LVL 39

Expert Comment

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

Thomas
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

618 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