| || |
- 1 Post By scsvel
- 2 Post By NoUse4aName
- 1 Post By dlai
- 1 Post By bklabel1
- 1 Post By belewda
How to get number separately from a string in VBscript
I am new to this forum and learning QTP VBScript.
Can anyone help me out to display numbers and string separately from the given input.
Dim strNumbers, strLetters
strNumbers = "" : strLetters = ""
For intChar = 1 To Len(strInput)
If IsNumeric(Mid(strInput, intChar, 1)) Then
strNumbers = strNumbers & Mid(strInput, intChar, 1)
strLetters = strLetters & Mid(strInput, intChar, 1)
SplitNumAndText = strNumbers & " and " & strLetters
Thank/Like to help others if my input helped you !!!
I am speaking for myself, not for my employer nor any one. Automation Ecstasy
Just use regex to do a global replace rather than loop through.
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Pattern = "[^\d]"
RegEx.IgnoreCase = True
RegEx.Global = True
Thanks all for the response!
There may be a way to connect to an Excel object. Use the functions in Excel such as Val(). I'm not sure exactly how to go about it but it is another idea.
I don't really recommend you do it this way but, if you're so inclined, here's some code to calculate a formula in Excel and return the value. It's cobbled together and ugly but I think it'll work.
Edit: fixed a runtime error when the formula isn't good. also, noticed that this won't work for what the OP wanted. none of the excel functions i tried seem to do the job without complaining.
Public Function ExcelCalc(sFormula)
filepath = "c:\temp\"
filename = "ExcelCalc.xls"
Set xl = CreateObject("Excel.Application")
xl.DisplayAlerts = false
set book = xl.Workbooks.Add
If mid(sFormula, 1, 1) <> "=" Then
sFormula = "=" & sFormula
xl.Cells(1, 1).Value = sFormula
calcTimeout = 30000 ' give it 30 seconds
iTimer = 0
While xl.Application.CalculationState = 1 and iTimer < calcTimeout
iTimer = iTimer + 25
If iTimer > calcTimeout Then
reporter.ReportEvent micFail, "CalcWorkbook Timeout", "CalcWorkbook Timeout exceeded."
If xl.Cells(1, 1).Value <> "#NAME?" and xl.Cells(1, 1).Value <> "#DIV/0!" and xl.Cells(1, 1).Value <> "#VALUE!" Then
ExcelCalc = xl.Cells(1, 1).Value
ExcelCalc = ""
Set book = nothing
Set xl = nothing
Last edited by belewda; 05-26-2015 at 06:25 AM.
I just threw the formula idea out there. I appreciate seeing the code.
I hope Mozhi lets us know which direction he went with the code.