+ Reply to Thread
Results 1 to 8 of 8

Challenge Parsing characters in a String

Hybrid View

  1. #1
    GS
    Guest

    RE: Challenge Parsing characters in a String

    Hi VectorZ,

    You could try something like this. (modify to suit)

    Sub ParseMyString3()
    Dim s1 As String, s2 As String, s3 As String, s4 As String
    Dim iPos As Integer
    Dim lLastRow As Long, r As Long


    s1 = "A##-#S"
    s2 = "A#-#S"
    s3 = "ARG###"
    s4 = "FO"

    With Range("A:A")
    lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    For r = 1 To lLastRow
    If .Cells(r, 1) <> "" Then
    iPos = 0
    If .Cells(r).Value Like "*" & s1 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    ElseIf .Cells(r).Value Like "*" & s2 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 4)
    ElseIf .Cells(r).Value Like "*" & s3 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    ElseIf .Cells(r).Value Like "*" & s4 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 2)
    End If
    End If
    Next r
    End With
    End Sub

    HTH
    Regards,
    Garry

  2. #2
    VectorZ
    Guest

    Re: Challenge Parsing characters in a String

    GS Thank you VERY much. That did the trick. The only issue left is that
    the prefix of A in the pattern can change form an A through Z in
    character(see example of characters). Changed to this. One thing I'll
    have to remember is when adding common patterns to add the longer ones
    first to filter before going to the shorter common patterns (see
    example of patterns).

    Example of Characters:
    AB2021A24-2S has an A24-2S but the A before the 24-2S can range from an
    A to Z. The
    BC2003ARG142 can be filtered off of RG and same with FO and many others

    Example of Patterns:
    s1 = "###-#S"
    s2 = "##-#S"
    s3 = "#-#S"

    Changed Code and works great:

    Sub ParseMyString3()
    Dim s1 As String, s2 As String, s3 As String, s4 As String
    Dim iPos As Integer
    Dim lLastRow As Long, r As Long

    s1 = "##-#S"
    s2 = "#-#S"
    s3 = "RG###"
    s4 = "FO"

    With Range("A:A")
    lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    For r = 1 To lLastRow
    If .Cells(r, 1) <> "" Then
    iPos = 0
    If .Cells(r).Value Like "*" & s1 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    ElseIf .Cells(r).Value Like "*" & s2 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 4)
    ElseIf .Cells(r).Value Like "*" & s3 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    ElseIf .Cells(r).Value Like "*" & s4 Then
    .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 2)
    End If
    End If
    Next r
    End With
    End Sub


  3. #3
    GS
    Guest

    Re: Challenge Parsing characters in a String

    You're welcome!

    I wasn't sure about the consistency of the "actual" pattern so I went with
    what you posted to demonstrate differentiating between similar patterns. (ie.
    s2 is found in s1)Your solution to modify the patterns exact and order them
    as you have is a good idea.

    Regards,
    Garry

  4. #4
    GS
    Guest

    RE: Challenge Parsing characters in a String

    A correction is needed for s4, it should be s4 = "-FO"

    Regards,
    Garry



    "GS" wrote:

    > Hi VectorZ,
    >
    > You could try something like this. (modify to suit)
    >
    > Sub ParseMyString3()
    > Dim s1 As String, s2 As String, s3 As String, s4 As String
    > Dim iPos As Integer
    > Dim lLastRow As Long, r As Long
    >
    >
    > s1 = "A##-#S"
    > s2 = "A#-#S"
    > s3 = "ARG###"
    > s4 = "FO"
    >
    > With Range("A:A")
    > lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    > For r = 1 To lLastRow
    > If .Cells(r, 1) <> "" Then
    > iPos = 0
    > If .Cells(r).Value Like "*" & s1 Then
    > .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    > ElseIf .Cells(r).Value Like "*" & s2 Then
    > .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 4)
    > ElseIf .Cells(r).Value Like "*" & s3 Then
    > .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 5)
    > ElseIf .Cells(r).Value Like "*" & s4 Then
    > .Cells(r, 2).Value = Right$(.Cells(r, 1).Value, 2)
    > End If
    > End If
    > Next r
    > End With
    > End Sub
    >
    > HTH
    > Regards,
    > Garry


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1