Consulting

Results 1 to 3 of 3

Thread: Split Function - Allow for variances

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    Split Function - Allow for variances

    I'm pulling ID numbers from a text string using the split function.

    The problem is that the original string source is from a scanned document and sometimes the characters were incorrectly converted to text.

    The full correct string preceding the ID I want to extract is "CarID(s):". In the example below a "~" character is where a "s" should be, I used error handling below to illustrate. This works for this specific case, but of course would not be a solution as would not know what the error would be before running the code.

    As long as a primary portion of the string was intact, say "CarID", is there a method that could be used to accommodate variations in the rest of text?

    On Error Resume Next 
    
    CarID = Trim(Split(Split(strText, "CarID(s):")(1), vbCrLf)(0))     'CarID(s) - Correct
    CarID = Trim(Split(Split(strText, "CarID(~):")(1), vbCrLf)(0))    'CarID(~) - Incorrect
    
    On Error GoTo 0

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Maybe using Left() and Right() ?


    Option Explicit
    Sub test()
        Dim s As String, CarID As String
        Dim p1 As String, p2 As String
        Dim i As Long
        
        s = "CarID(s):123412341234"
        
        i = InStr(s, ":")
        If i > 0 Then
            p1 = Left(s, i)
            p2 = Right(s, Len(s) - i)
        
            MsgBox p1 & " -- " & p2
        End If
        
        
        s = "CarID(~):123412341234"
        
        i = InStr(s, ":")
        If i > 0 Then
            p1 = Left(s, i)
            p2 = Right(s, Len(s) - i)
        
            MsgBox p1 & " -- " & p2
        End If
        
        
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    That's great, I will work with that - thanks!

Posting Permissions

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