ckelsoe
asked on
Return middle part of a string
I have a string that looks like the following:
firstword_secondword_third word_TheWordIWant_fifthword_sixthword_seven thword.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
firstword_secondword_third
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
The intUBound line is redundant (I modified this from another function that used it)
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
ASKER
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.
Thanks for the quick response.
Glad to help. Thanks for the grade.
Thomas
Thomas
http://www.ozgrid.com/VBA/extract-words-function.htm
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.WorksheetFunct
lWordCount = Len(text_string) - Len(.Substitute(text_strin
If IsNumeric(nth_word) Then
nth_word = nth_word - 1
Get_Word = Mid(Mid(Mid(.Substitute(te
.Find("^", .Substitute(text_string, " ", "^", nth_word)), 256), 2, _
.Find(" ", Mid(Mid(.Substitute(text_s
.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_strin
Len(.Substitute(text_strin
Len(text_string) - Len(.Substitute(text_strin
End If
End With
End Function