Results 1 to 2 of 2

Thread: Grab the word/value from cell staring with "DWB"

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width