Extract number from string in VBA

后端 未结 4 1497
攒了一身酷
攒了一身酷 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:16

    This will extract the first number in a string:

    Public Function GetNumber(s As String) As Long
        Dim b As Boolean, i As Long, t As String
        b = False
        t = ""
        For i = 1 To Len(s)
            If IsNumeric(Mid(s, i, 1)) Then
                b = True
                t = t & Mid(s, i, 1)
            Else
                If b Then
                    GetNumber = CLng(t)
                    Exit Function
                End If
            End If
        Next i
    End Function
    

    enter image description here

提交回复
热议问题