Extract number from string in VBA

后端 未结 4 1500
攒了一身酷
攒了一身酷 2020-12-21 09:51

I have cells in vba that contain strings like this:

QUANTITY SUPPLY <= DAYS SUPPLY|30 IN 23 DAYS

I send these strings through two functions that just pick

4条回答
  •  庸人自扰
    2020-12-21 10:17

    You might pass an optional parameter in to distinguish which number you want to pull.

    Public Function days_supply(blurb As String, Optional i As Long = 1)
        Dim sTMP As String
        sTMP = Trim(Split(blurb, "|")(1))
    
        If i = 1 Then
            days_supply = CLng(Trim(Left(Replace(sTMP, " ", Space(99)), 99)))
        Else
            sTMP = Trim(Mid(sTMP, InStr(1, LCase(sTMP), " in ", vbTextCompare) + 4, 9))
            days_supply = CLng(Trim(Left(Replace(sTMP, " ", Space(99)), 99)))
        End If
    End Function
    

        VBA text parsing Split

    The formula in B1 is,

    =days_supply(A1)
    

    The formula in C1 is,

    =days_supply(A1,2)
    

提交回复
热议问题