NumberBeforeString

Cuts a number that would be found before certain word inside a block string.
This is used to read number from webpage, like the number that would be found before the word "employees".
It will keep searching until it finds a number that is right before that word.
So far, it uses the space as a separator between the word and the letter.

Edit: 2024-02-14: Adding ability to search for other forms of space, HTML spaces like xa0, nbsp, 160

CodeFunctionName
What is this?

Public

Tested

Original Work
Function NumberBeforeString(FindText, InString, Optional SepaS = " |:| | | ", Optional ByRef FullStatement = "")
    ' Reads the number if found that can be found before the string in a larger string
    ' Only if separators were found
    ' Needs HTML_CleanNumber()
    '
    Rett = ""
    X1 = 1
    Found1 = 0
    Do
        nextfound = VBInstr(FindText, InString, X1)
        If nextfound = 0 Then Exit Do
        For Each Sepa In Split(SepaS, "|")
            If UCase(Mid(InString, nextfound - Len(Sepa), Len(Sepa))) < > UCase(Sepa) Then GoTo NextSepa
            Sepa1 = VBInstr_Rev(Sepa, InString, nextfound - 1)
            If Sepa1 = 0 Then Exit For
            XB1 = 1
GoBehindNumber:
            SepaB = CutString3(SepaS, XB1, "|")
            If SepaB = SepaS Then Exit For
            Sepa2 = VBInstr_Rev(SepaB, InString, Sepa1 - 1)
            If Sepa2 = 0 Then Exit For
            PossibleNumber = CutString1(InString, Sepa2, Sepa1 - 1)
            If IsNumeric(PossibleNumber) Then
                Found1 = 1
                Exit For
            Else
                XB1 = XB1 + 1
                GoTo GoBehindNumber
            End If
NextSepa:
        Next
       
        If Found1 = 1 Then
            Rett = Val(HTML_CleanNumber(Trim(PossibleNumber)))
            FullStatement = PossibleNumber & CutString1(InString, Sepa1 + 1, VBInstr(Sepa, InString, nextfound)) ' Save location where we found it to show page to user for that location
            Exit Do
        End If
        X1 = nextfound + 2
    Loop
    NumberBeforeString = Rett
End Function

FindText, InString, Optional SepaS = " "

Views 229

Downloads 58

CodeID
DB ID