I'm trying to automate a script using QTP and SAP.
In SAP an order is generated with Document number in status bar as "Standard PO created under the number 4500290636"
My challenge is how should I convert take string to an Integer value.
I'm trying to automate a script using QTP and SAP.
In SAP an order is generated with Document number in status bar as "Standard PO created under the number 4500290636"
My challenge is how should I convert take string to an Integer value.
Since you are using SAP, I think it is safe to assume that the document number will always have a length of 10 characters. So, you can extract it using the Right function, then just convert it using Val. Something like this:
yourInteger = Val(Right(yourSAPString, 10))
Note: in .net, you could use Convert.ToInt32 instead of Val
You could use the split 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))
Then test for numeric, will allow for many lengths and could change the position of the number in the returned values.
I would recommend using a custom function.
Here is the function:
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
Here is the usage example:
Sub Example()
Debug.Print ExtractNumber("Standard PO created under the number 4500290636")
End Sub
This code was taken from a similar, more recent answer that has better solutions. See here: Excel VBA - Extract numeric value in string