|
-
Jan 31st, 2013, 11:51 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Get values from a string
i am trying to get values from long strings of different lengths in a listbox;
Win Runs: 7329 high dollar amount 41.25 low credit $12.47 Result $41.48
Loss Runs: 6856 high dollar amount 25.80 low credit $40.52 Result $40.52
Win Runs: 136 high dollar amount 40.75 low credit $13.47 Result $40.52
Win Runs: 1354 high dollar amount 40.25 low credit $15.47 Result $40.00
Loss runs 13111 high dollar amount 35.80 low credit $41.48 Result $41.48
I am trying to addup all the runs eg:7329+6856+136+1354+13111 = 28,786
And
Low Credit $12.47+$40.52+$13.47+$15.47+$41.48 = 107.94
How can this be done?
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Jan 31st, 2013, 11:54 PM
#2
Fanatic Member
Re: Get values from a string
parsing the input?
what is the source of text lines? keyboard or a text file?.
-
Feb 1st, 2013, 12:08 AM
#3
Thread Starter
PowerPoster
Re: Get values from a string
Values are loaded from a file as strings
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 1st, 2013, 12:10 AM
#4
Re: Get values from a string
There are several ways of doing this, one of which is to use the Replace function to substitute all know text values with a delimiter and then use the Split function on the result
i.e.
s="Win Runs: 7329 high dollar amount 41.25 low credit $12.47 Result $41.48"
s=Replace(s,"Win Runs: ",vbnullstring)
s=Replace(s,"high dollar amount ","|")
s=Replace(s,"low credit $","|")
etc.
and you'll then be left with a string that looks like "7329|41.25|12.47|41.48"
The Split function will, of course, create an array of the four values. I'm sure you get the idea...
Define your Replace strings as constants for better code readability and ease-of-maintenance...
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Feb 1st, 2013, 12:25 AM
#5
Re: Get values from a string
start with this
Code:
Private Sub Command1_Click()
Dim sText As String
Dim TextLine() As String
Dim NeededNumbers() As String
Dim AllRuns As Currency
Dim AllLowCredit As Currency
Dim i As Integer, j As Integer
sText = "Win Runs: 7329 high dollar amount 41.25 low credit $12.47 Result $41.48" & vbNewLine & _
"Loss Runs: 6856 high dollar amount 25.80 low credit $40.52 Result $40.52" & vbNewLine & _
"Win Runs: 136 high dollar amount 40.75 low credit $13.47 Result $40.52" & vbNewLine & _
"Win Runs: 1354 high dollar amount 40.25 low credit $15.47 Result $40.00" & vbNewLine & _
"Loss runs 13111 high dollar amount 35.80 low credit $41.48 Result $41.48"
TextLine = Split(sText, vbNewLine)
For i = 0 To UBound(TextLine)
NeededNumbers = Split(TextLine(i), Space(1))
For j = 0 To UBound(NeededNumbers)
If j = 2 Then AllRuns = AllRuns + NeededNumbers(j)
If j = 6 Then AllLowCredit = AllLowCredit + NeededNumbers(j)
Next j
Next i
Debug.Print AllRuns
Debug.Print AllLowCredit
End Sub
-
Feb 1st, 2013, 12:29 AM
#6
Re: Get values from a string
for all of them do this
Code:
Private Sub Command1_Click()
Dim sText As String
Dim TextLine() As String
Dim NeededNumbers() As String
Dim AllRuns As Currency
Dim AllLowCredit As Currency
Dim AllHighDollar As Currency
Dim AllResult As Currency
Dim i As Integer, j As Integer
sText = "Win Runs: 7329 high dollar amount 41.25 low credit $12.47 Result $41.48" & vbNewLine & _
"Loss Runs: 6856 high dollar amount 25.80 low credit $40.52 Result $40.52" & vbNewLine & _
"Win Runs: 136 high dollar amount 40.75 low credit $13.47 Result $40.52" & vbNewLine & _
"Win Runs: 1354 high dollar amount 40.25 low credit $15.47 Result $40.00" & vbNewLine & _
"Loss runs 13111 high dollar amount 35.80 low credit $41.48 Result $41.48"
TextLine = Split(sText, vbNewLine)
For i = 0 To UBound(TextLine)
NeededNumbers = Split(TextLine(i), Space(1))
For j = 0 To UBound(NeededNumbers)
If j = 2 Then AllRuns = AllRuns + NeededNumbers(j)
If j = 6 Then AllHighDollar = AllHighDollar + NeededNumbers(j)
If j = 9 Then AllLowCredit = AllLowCredit + NeededNumbers(j)
If j = 11 Then AllResult = AllResult + NeededNumbers(j)
Next j
Next i
Debug.Print AllRuns
Debug.Print AllHighDollar
Debug.Print AllLowCredit
Debug.Print AllResult
End Sub
tested but not tested to make sure every calculation is right (should be)
-
Feb 1st, 2013, 01:00 AM
#7
Thread Starter
PowerPoster
Re: Get values from a string
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 1st, 2013, 10:55 AM
#8
Re: Get values from a string
 Originally Posted by Max187Boucher
Code:
For i = 0 To UBound(TextLine)
NeededNumbers = Split(TextLine(i), Space(1))
For j = 0 To UBound(NeededNumbers)
If j = 2 Then AllRuns = AllRuns + NeededNumbers(j)
If j = 6 Then AllHighDollar = AllHighDollar + NeededNumbers(j)
If j = 9 Then AllLowCredit = AllLowCredit + NeededNumbers(j)
If j = 11 Then AllResult = AllResult + NeededNumbers(j)
Next j
Next i
No need for a second loop if you do it that way. Just refer to the elements of 'NeededNumbers' like this:
Code:
For i = 0 To UBound(TextLine)
NeededNumbers = Split(TextLine(i), Space(1))
AllRuns = AllRuns + NeededNumbers(2)
AllHighDollar = AllHighDollar + NeededNumbers(6)
AllLowCredit = AllLowCredit + NeededNumbers(9)
AllResult = AllResult + NeededNumbers(11)
Next i
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Feb 1st, 2013, 11:06 AM
#9
Thread Starter
PowerPoster
Re: Get values from a string
Thanks for adding that I have decided to go with this:
Sub main()
Dim str As String
Dim run As String
str = "Win Runs: 7329 high dollar amount 41.25 low credit $12.47 Result $41.48"
runs = GetNumAfter(str, "Runs: ")
amount = GetNumAfter(str, "amount ")
credit = GetNumAfter(str, "credit $")
result = GetNumAfter(str, "Result $")
End Sub
Public Function GetNumAfter(MainString As String, SearchString As String)
Dim ResultValue As String
Dim pos1 As Integer
Dim pos2 As Integer
pos1 = InStr(MainString, SearchString) + Len(SearchString)
pos2 = InStr(pos1, MainString, " ")
If (pos2 = 0) Then pos2 = Len(MainString) + 1
ResultValue = Mid(MainString, pos1, pos2 - pos1)
GetNumAfter = ResultValue
End Function
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 1st, 2013, 11:25 AM
#10
Re: [RESOLVED] Get values from a string
There ya go. You knew how to do it all along ;-)
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|