Grab the word/value from cell staring with "DWB"
Hi,
I have to name my sheet with a specific word/value from cell A1.
In A1 I have a sentence with a code that always starts with "DWB" (eg: DWB203232013).
Let say it is this: "Radjesh needs to filter the value DWB20130328 from this sentence"
How do I "grab" that word/value?
Thanks in advance.
Re: Grab the word/value from cell staring with "DWB"
Something like this (assumes there is a space after the "code" you need to identify):
Code:
Sub grabVal()
Dim i As Integer
Dim j As Integer
Dim phraseLength As Integer
Dim phraseToCheck As String
Dim myCode As String
phraseToCheck = Range("a1").Value
phraseLength = Len(phraseToCheck)
For i = 1 To phraseLength - 2
If Mid(phraseToCheck, i, 3) = "DWB" Then
'check for next space
For j = i + 3 To phraseLength
If Mid(phraseToCheck, j, 1) = " " Then
'found space
myCode = Mid(phraseToCheck, i, j - i)
GoTo BailOut
End If
If j = phraseLength And myCode = "" Then
'in this case the code went to the end of the phrase
myCode = Mid(phraseToCheck, i, phraseLength - i) 'check the math on this one
GoTo BailOut
End If
Next j
End If
Next i
BailOut:
MsgBox myCode
End Sub