Results 1 to 4 of 4
  1. #1
    3 Star Lounger MacroAlan's Avatar
    Join Date
    Feb 2003
    Location
    St Louis, Missouri, USA
    Posts
    254
    Thanks
    6
    Thanked 1 Time in 1 Post

    Finding Text within String (2000/SP-3/VBA)

    I am reading in this big TEXT file and looking for specific statuses. I just discovered that the line that I need to pull invoice numbers is centered within the row.

    I guess my question is: Is there a way to get the position number where the thing starts AND is there a way to get around the error when the line does not contain this thing?

    <pre> Open DuckToOpen For Input As #3
    Do While Not EOF(3)
    Line Input #3, TotalCost
    ' Debug.Print TotalCost
    'Debug.Print "1234567890123456789012345678901234567890123456789 012345678901234567890"
    daPlace = Application.WorksheetFunction.Find("Job Number: ", TotalCost, 1)
    <font color=red>'Errors out here if "Job Number" is not on row</font color=red>
    Cells(PasteRow, 2) = Chr(39) & Mid(TotalCost, daPlace+12, 8)
    </pre>

    Alan

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Finding Text within String (2000/SP-3/VBA)

    Use INSTR, it gives a zero when not found.

    (Beware "air code" for example)

    daPlace = INSTR(TotalCost, "Job Number: ")
    If daplace <> 0 then
    Cells(PasteRow, 2) = Chr(39) & Mid(TotalCost, daPlace+12, 8)
    else 'if desired or remove
    'do whatever you want when not found
    end if

    Steve

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding Text within String (2000/SP-3/VBA)

    Try something like this:

    <pre>Dim daPlace As Long
    daPlace = InStr(TotalCost, "Job Number: ")
    If daPlace > 0 Then
    Cells(PasteRow, 2) = Chr(39) & Mid(TotalCost, daPlace+12, 8)
    End If
    </pre>

    Legare Coleman

  4. #4
    3 Star Lounger MacroAlan's Avatar
    Join Date
    Feb 2003
    Location
    St Louis, Missouri, USA
    Posts
    254
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Finding Text within String (2000/SP-3/VBA)

    You guys are the greatest! <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35>
    Alan

Posting Permissions

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