Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA从字符串中获取整数值_Vba - Fatal编程技术网

使用VBA从字符串中获取整数值

使用VBA从字符串中获取整数值,vba,Vba,我正在尝试使用QTP和SAP自动化脚本 在SAP中,生成订单时,状态栏中的文档编号为“根据编号4500290636创建的标准采购订单” 我的挑战是如何将take字符串转换为整数值。由于您使用的是SAP,我认为可以安全地假设文档编号的长度始终为10个字符。因此,您可以使用Right函数提取它,然后使用Val转换它。大概是这样的: yourInteger = Val(Right(yourSAPString, 10)) 注意:在.net中,您可以使用Convert.ToInt32而不是Val您可以使

我正在尝试使用QTP和SAP自动化脚本

在SAP中,生成订单时,状态栏中的文档编号为“根据编号4500290636创建的标准采购订单”


我的挑战是如何将take字符串转换为整数值。

由于您使用的是SAP,我认为可以安全地假设文档编号的长度始终为10个字符。因此,您可以使用
Right
函数提取它,然后使用
Val
转换它。大概是这样的:

yourInteger = Val(Right(yourSAPString, 10))

注意:在.net中,您可以使用Convert.ToInt32而不是Val

您可以使用split函数:

Dim strSplit As Variant
Dim yourNumericOut As Variant

    strSplit = Split("Standard PO created under the number 4500290636")

    If IsNumeric(UBound(strSplit)) Then yourNumericOut = --strSplit(UBound(strSplit))
Public Function ExtractNumber(fromString As String) As Double
    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Pattern = "(\d{1,3},?)+(\.\d{2})?"
        .Global = True
        If .Test(fromString) Then
            ExtractNumber = CDbl(.Execute(fromString)(0))
        End If
    End With
End Function

然后测试数值,将允许许多长度,并可以更改返回值中数字的位置。

我建议使用自定义函数

以下是函数:

Dim strSplit As Variant
Dim yourNumericOut As Variant

    strSplit = Split("Standard PO created under the number 4500290636")

    If IsNumeric(UBound(strSplit)) Then yourNumericOut = --strSplit(UBound(strSplit))
Public Function ExtractNumber(fromString As String) As Double
    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Pattern = "(\d{1,3},?)+(\.\d{2})?"
        .Global = True
        If .Test(fromString) Then
            ExtractNumber = CDbl(.Execute(fromString)(0))
        End If
    End With
End Function
以下是使用示例:

Sub Example()
    Debug.Print ExtractNumber("Standard PO created under the number 4500290636")
End Sub
这段代码取自一个类似的、更近期的答案,该答案有更好的解决方案。请看这里:

您的数字字符串是否总是出现在末尾,是否总是10位数?