PDA

View Full Version : [SOLVED] Split Function - Allow for variances



BrI
02-19-2019, 10:20 AM
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

Paul_Hossler
02-19-2019, 10:39 AM
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

BrI
02-19-2019, 11:07 AM
That's great, I will work with that - thanks!