Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Public Function Extract(Source As String, LeftExtracts As String, RightExtracts As String, Optional StartFrom As String = "1", Optional ExtractAll As Boolean = False) As String
  3.         On Error Resume Next
  4.  
  5. ' ################## '
  6. ' #  Declarations  # '
  7. ' ################## '
  8.  
  9.         Dim lngLeft As Long
  10.         Dim lngRight As Long
  11.         Dim strReturned As String
  12.         Dim lngExtracts(0 To 2) As Long
  13.         Dim TempText(0 To 2) As String
  14. ' ------------------------------------------------------------ '
  15.  
  16.         Select Case ExtractAll
  17.  
  18.                 Case False
  19.                         If StartFrom <= 0 Then
  20.                                 ' Invalid StartFrom point '
  21.                                MsgBox "Error:" & vbNewLine & vbNewLine _
  22.                                                 & "Invalid Start Point"
  23.                                 Exit Function
  24.                         ElseIf Source = Empty Then
  25.                                 ' Nothing in the source '
  26.                                MsgBox "Error:" & vbNewLine & vbNewLine _
  27.                                                 & "Nothing to compare with.."
  28.                                 Exit Function
  29.                         End If
  30.  
  31.                         ' Gets the numbering of the left string for Mid '
  32.                        lngExtracts(0) = InStr(StartFrom, Source, LeftExtracts, vbTextCompare) + Len(LeftExtracts)
  33.                         ' Gets the numbering of the right string for Mid '
  34.                        lngExtracts(1) = InStr(StartFrom, Source, RightExtracts, vbTextCompare)
  35.  
  36.                         ' A Few Checks Below '
  37.  
  38.                         ' Left String '
  39.                        If LeftExtracts = Empty Then
  40.                                 MsgBox "Error:" & vbNewLine & vbNewLine _
  41.                                                 & "Left Extraction String Not Found..."
  42.                                 Exit Function
  43.                         ' Right String '
  44.                        ElseIf RightExtracts = Empty Then
  45.                                 MsgBox "Error:" & vbNewLine & vbNewLine _
  46.                                                 & "Right Extraction String Not Found..."
  47.                         End If
  48.  
  49.                         ' Gets the String in the middle of both strings in LeftExtracts, and RightExtracts '
  50.                        Extract = Mid$(Source, lngExtracts(0), lngExtracts(1) - lngExtracts(0))
  51.  
  52.                         ' One last check to make sure all goes well '
  53.                        If Extract = Empty Then
  54.                                 Extract = "No Text could be returned..."
  55.                         End If
  56.  
  57.                         Exit Function
  58.  
  59.  
  60.                 Case True
  61. 'Extract All
  62. MsgBox "Not coded yet.."
  63.         End Select
  64.  
  65. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement