Hi,
I am working on a project to strip invoice numbers from email data for my job. Using different VBA Scripts, I have managed to create a function that find the next 8 digit number after the substring "invoice". However, I want to expand the function to cover a variation of words ("inv", "bill number") but I am stuck on how to. I know that I can't have multiple strings to look for with the function INSTR and I think I need an array. I am stuck on how to rework this vba.
I am working on a project to strip invoice numbers from email data for my job. Using different VBA Scripts, I have managed to create a function that find the next 8 digit number after the substring "invoice". However, I want to expand the function to cover a variation of words ("inv", "bill number") but I am stuck on how to. I know that I can't have multiple strings to look for with the function INSTR and I think I need an array. I am stuck on how to rework this vba.
VBA Code:
Public Function GetInvNum(cell As Range)
Dim s As String
Dim i As Integer
Dim answer
Dim counter As Integer
Dim CharPos As Long
'get cell value
s = cell.Value
'set the counter
counter = 0
CharPos = InStr(1, s, "invoice", vbTextCompare) + 2
If CharPos > 2 Then
'loop through the entire string
For i = CharPos To Len(s)
'check to see if the character is a numeric one
If IsNumeric(Mid(s, i, 1)) = True Then
'add it to the answer
answer = answer + Mid(s, i, 1)
counter = counter + 1
'check to see if we have reached 8 digits
If counter = 8 Then
GetInvNum = answer
Exit Function
End If
Else
'was not numeric so reset counter and answer
counter = 0
answer = ""
End If
Next i
End If
End Function
Last edited by a moderator: