Consulting

Results 1 to 5 of 5

Thread: Extracting a number from text using a vba function

  1. #1

    Extracting a number from text using a vba function

    Hi,

    I have a formula:
    =IFERROR(IF(BJ3>Y3,M3,VLOOKUP(F3,'Bid Template'!L:CU,88,FALSE)),M3)
    This returns SPEC-8 or BID-10 or SPEC-4 or BID-1.5, ETC

    All I want to do is put a function around the outside of this formula that extracts only the number.

    So it would read 8 if the result of the inside formula was SPEC-8 or 1.5 if it was BID-1.5.

    I'm wondering if there's a custom VBA function to do it that would look like this:

    =ExtractNumber(IFERROR(IF(BJ3>Y3,M3,VLOOKUP(F3,'Bid Template'!L:CU,88,FALSE)),M3))
    Any ideas?

    Thanks!

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    need to know what is the value of M3? The UDF must also deal with it.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    =IFERROR(IF(BJ3>Y3,M3,MID(VLOOKUP(F3, 'Bid Template'!L:CU,88,FALSE),FIND("-",VLOOKUP(F3, 'Bid Template'!L:CU,88,FALSE))+1,99)),M3)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Several ways to do it. In your case, taken literally, all those numbers would be negative. Luckily, some methods ignore the negative sign. Obviously, use GetNumber rather than your ExtractNumber or modify the UDF's name.
    e.g.
    Sub Test_GetNumber()  MsgBox GetNumber("BID-1.5")
    End Sub
    'Derk, ozgrid.com, 65763
    Function GetNumber(s As String)
        Dim j As Long
        While Not IsNumeric(Left(s, 1))
            If Len(s) <= 1 Then
                Exit Function
            Else
                s = Mid(s, 2)
            End If
        Wend
        GetNumber = val(s)
    End Function

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    =F_snb(IFERROR(If(BJ3>Y3,M3,VLOOKUP(F3, 'Bid Template'!L:CU,88,FALSE)),M3))

    Function F_snb(c00)
       F_snb=""
       if instr(c00,"-") then F_snb=split(c00,"-")(1)
    End Function

Posting Permissions

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