warheat001
asked on
Code for this
Hi,
Could some one help me in coding here. I am not very good at these stuff. So anyone could help.
I tried, but I am getting either a wrong value or a zero.
strData = "Location1|22.2|Location2| 22.3|Locat ion3|22.4| Location4| 22.5"
'This is a string that I retrieve from a url. The string is always the same except for the values and name of location.
'The separator is always "|" character.
'The "Location1|22.2" is one data. And "Location2|22.3" is the second data. Like that four data.
'What I want to do is, I want to write a function in which when I call to this function and pass the location (1, 2, 3 or 4)
'I want to get the respetive value for that location.
Function Get_Value(Location as Integer)
'I want to know the code here...
End Function
Could some one help me in coding here. I am not very good at these stuff. So anyone could help.
I tried, but I am getting either a wrong value or a zero.
strData = "Location1|22.2|Location2|
'This is a string that I retrieve from a url. The string is always the same except for the values and name of location.
'The separator is always "|" character.
'The "Location1|22.2" is one data. And "Location2|22.3" is the second data. Like that four data.
'What I want to do is, I want to write a function in which when I call to this function and pass the location (1, 2, 3 or 4)
'I want to get the respetive value for that location.
Function Get_Value(Location as Integer)
'I want to know the code here...
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Option Explicit
Private Sub Command1_Click()
Dim strData As String
Dim dblData As Double
strData = "Location1|22.2|Location2| 22.3|Locat ion3|22.4| Location4| 22.5"
dblData = Get_Value(strData, 4)
MsgBox dblData
End Sub
Private Function Get_Value(ByVal strData As String, ByVal Location As Integer) As Double
On Error GoTo badConversion
Dim values As Variant
Dim index As Integer
index = (Location * 2) - 1
values = Split(strData, "|")
If index >= LBound(values) And index <= UBound(values) Then
Get_Value = CDbl(values(index))
Else
MsgBox "Location: " & Location & vbCrLf & vbCrLf & strData, vbCritical, "Invalid Location for Data"
End If
Exit Function
badConversion:
MsgBox "Value: " & values(index), vbCritical, "Unable to convert value to Double"
End Function
Private Sub Command1_Click()
Dim strData As String
Dim dblData As Double
strData = "Location1|22.2|Location2|
dblData = Get_Value(strData, 4)
MsgBox dblData
End Sub
Private Function Get_Value(ByVal strData As String, ByVal Location As Integer) As Double
On Error GoTo badConversion
Dim values As Variant
Dim index As Integer
index = (Location * 2) - 1
values = Split(strData, "|")
If index >= LBound(values) And index <= UBound(values) Then
Get_Value = CDbl(values(index))
Else
MsgBox "Location: " & Location & vbCrLf & vbCrLf & strData, vbCritical, "Invalid Location for Data"
End If
Exit Function
badConversion:
MsgBox "Value: " & values(index), vbCritical, "Unable to convert value to Double"
End Function
Private Function GetLocation(ByVal location As Integer, strdata As String) As String
Dim arr
arr = Split(strdata, "|")
GetLocation = arr(location + 1)
End Function
As an example:
Dim result
strdata = "Location1|22.2|Location2| 22.3|Locat ion3|22.4| Location4| 22.5"
result = GetLocation(2, strdata)
result now contains 22.3
Regards.
Dim arr
arr = Split(strdata, "|")
GetLocation = arr(location + 1)
End Function
As an example:
Dim result
strdata = "Location1|22.2|Location2|
result = GetLocation(2, strdata)
result now contains 22.3
Regards.
Lol...guess I'm a little sloooooow today.
=)
Idle_Mind
=)
Idle_Mind
it may b helpful 2 visit msdn website 4 getting up2date with all the functions in vb
ASKER
That is so cooool...
I never knew that there was a function called SPLIT.
Thanx TimCottee