Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search within a String and Extract one word based on one character 2

Status
Not open for further replies.

Dauphy

IS-IT--Management
Nov 8, 2001
111
CA
Hi!

I need help with locating one word within a string; the word starts with a K. examples: Sample Bag K5678l1 or
K5789012 Donation

I only need the K portion of the string; the rest is junk.

Thank you.

 
Dim Start As Integer
Start= InStr(1,MyString,"K",vbTextCompare)
If Start Then
Debug.Print Mid$(MyString,Start,InStr(Start,MyString," ")-Start)
End If

You could also use Split() twice: The first time on "K" and take the 2nd element, if one exists, and then Split again on a Space, and take the first element.
 
The easiest way - maybe not the most efficient is:

Dim j As Integer
Dim sData As String
Dim vData As Variant

'Test data
sData = "This is a test of the K1212121|0 string that has another K334343 value."

'Get the array of space delimited words into an array - this assumes data is split by spaces!
vData = Split(sData, " ")

'Check for more than one 'K' value - this assumes your K values start the word
For j = 0 To UBound(vData)
If Left(vData(j), 1) = "K" Then
'Here is your value vData(j)
sData = vData(j)
End If
Next j
 
That worked great! However, I can't figure out the syntax if the K was at the end of the word and not at the beginning.

Thank you.

 
Then just use the Right command - you can check for beginning or ending:
If Left(vData(j), 1) = "K" Or Right(vData(j), 1) = "K"

-------------------------- Code added
Dim j As Integer
Dim sData As String
Dim vData As Variant

'Test data
sData = "This is a test of the K1212121|0 string that has another K334343 value."

'Get the array of space delimited words into an array - this assumes data is split by spaces!
vData = Split(sData, " ")

'Check for more than one 'K' value - this assumes your K values start the word
For j = 0 To UBound(vData)
If Left(vData(j), 1) = "K" Or Right(vData(j), 1) = "K" Then
'Here is your value vData(j)
sData = vData(j)
End If
Next j
 
Thanks!!!! I'll get better at this yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top