Trošičku inak
EDIT: A pridávam ešte, ak by bolo potrebné, aj jedno z riešení vo VBA:
Function GetLog(Log As String, Param As String) As Long
Select Case Param
Case "l": GetLog = Evaluate("=VALUE(MID(""" & Log & """,7,FIND(""_ratio:"",""" & Log & """)-7))")
Case "r": GetLog = Evaluate("=VALUE(MID(""" & Log & """,FIND(""_ratio:"",""" & Log & """)+7,FIND(""_fix"",""" & Log & """)-(FIND(""_ratio:"",""" & Log & """)+7)))")
Case "f": GetLog = Evaluate("=VALUE(MID(""" & Log & """,FIND(""_fix:"",""" & Log & """)+5,FIND(""_adjust"",""" & Log & """)-(FIND(""_fix:"",""" & Log & """)+5)))")
Case "a": GetLog = Evaluate("=VALUE(RIGHT(""" & Log & """,LEN(""" & Log & """)-(FIND(""_adjust:"",""" & Log & """)+7)))")
End Select
End Function
A príklad použitia:
Sub Vysledok()
Dim s As String, hLimit As Long, hRatio As Long, hFix As Long, hAdjust As Long
s = "limit:999_ratio:0_fix:0_adjust:1"
hLimit = GetLog(s, "l")
hRatio = GetLog(s, "r")
hFix = GetLog(s, "f")
hAdjust = GetLog(s, "a")
MsgBox (hLimit & vbNewLine & hRatio & vbNewLine & hFix & vbNewLine & hAdjust)
End Sub
Ešte jeden iný spôsob ako to isté dosiahnuť:
Function GetLog(Log As String, Param As String) As Long
Dim SL() As String
SL = Split(Log, ":")
Select Case Param
Case "l": GetLog = CLng(Left(SL(1), InStr(1, SL(1), "_") - 1))
Case "r": GetLog = CLng(Left(SL(2), InStr(1, SL(2), "_") - 1))
Case "f": GetLog = CLng(Left(SL(3), InStr(1, SL(3), "_") - 1))
Case "a": GetLog = CLng(SL(4))
End Select
End Function
Ešte kratšie
Function GetLog(Log As String, Param As String) As Long
Dim SL() As String
SL = Split(Log, ":")
Select Case Param
Case "l", "r", "f": GetLog = CLng(Left(SL(InStr(1, "lrf", Param)), InStr(1, SL(InStr(1, "lrf", Param)), "_") - 1))
Case "a": GetLog = CLng(SL(4))
End Select
End Function
Dalo by sa ešte kratšie, posledný HardCore :
Function GetLog(Log As String, Param As String) As Long
If Param = "a" Then GetLog = CLng(Split(Log, ":")(4)) Else GetLog = CLng(Left(Split(Log, ":")(InStr(1, "lrf", Param)), InStr(1, Split(Log, ":")(InStr(1, "lrf", Param)), "_") - 1))
End Function
Ak môže byť Param číslo (číslo parametru 1=limit ...) tek
Function GetLog(Log As String, Param As Byte) As Long
If Param = 4 Then GetLog = CLng(Split(Log, ":")(4)) Else GetLog = CLng(Left(Split(Log, ":")(Param), InStr(1, Split(Log, ":")(Param), "_") - 1))
End Function
Škoda že to nežerie IIf(...) bolo by to ešte o pár znakov kratšie. Ak to má byť ale používané mnoho krát, tak tieto krátke sú nevýhodné, lebo Split robia viackrát.
Příloha: 25153_hodnotyztextu.rar (8kB, staženo 13x) citovat