Parsing string VBA

Viewed 61

please have at look on my issue. I have following string "F) GND G) FL240)" in my Control.

I'm trying to parse the string between "F)" and "G)" and also between "G)" and ")" with following code

If Left(s, 2) = "F)" Then
'Task - Filled ItemF  with value "GND"  or other values between  "F)" and "G)"  
        ItemF = Mid(s, InStr(s, "F)") + 3, Len(s) - InStr(s, "G)"))

'or

        'ItemF = Mid(s, InStr(s, "F)") + 3, Len(s) - Len(Right(s, InStr(s, "G)"))))

'Task - Filled ItemG  with value "FL240"  or other values between  "G)" and ")" 

 
            ItemG = Mid(s, InStr(s, "G)") + 3, Len(s) - InStr(s, "G)")) - how I can exclude last ")" 
        End If

Could you explain please, how to separate and parse values between "F)" and "G)" and also between "G)" and ")"

2 Answers

Use Split:

FirstValue = Split(Split(s, "F) ")(1), " G)")(0)
SecondValue = Split(Split(s, "G) ")(1), ")")(0)

Use this function -

Public Function fParseXML(sTag01 As String, sTag02 As String, sToParse As String) As String
 Dim pStart As Integer
 Dim pEnd As Integer
 Dim pLen As Integer
 If InStr(1, sToParse, sTag01) Then
  pStart = InStr(1, sToParse, sTag01) + Len(sTag01)
  pEnd = InStr(pStart, sToParse, sTag02)
  pLen = pEnd - pStart
  fParseXML = Mid(sToParse, pStart, pLen)
  Exit Function
 Else
  fParseXML = "Not found" & "**" & sTag01 & "**" & Mid(sToParse, 1, 100)
  Exit Function
 End If

use F) and G) and ) as the tags.

Related