excel vba- extract text between 2 characters

前端 未结 4 867
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 15:35

If i had this column:

ColA
-----
NUMBER(8,3)
NUMBER(20)

I need a VBA function that would go (note these start and end string would only eve

4条回答
  •  无人及你
    2020-12-01 15:57

    I know this question has been marked answered, but i thought i will add my code based on the updated question's function. I made a function that will allow it to parse out multiple.

    Sub testextract()
     s = extract_values("This is [what i want to keep][And this]")
     Debug.Print (s)
      End Sub
     Function extract_values(str As String, Optional openStr = "[", Optional closeStr = "]") As String
     Dim openPos As Integer
     Dim closePos As Integer
     Dim midBit As String
     prevOpen = 1
     prevClose = 1
     Dim keep As String
     While prevOpen <> 0
        On Error Resume Next
        openPos = InStr(prevOpen, str, openStr)
        On Error Resume Next
       closePos = InStr(prevClose, str, closeStr)
        On Error Resume Next
        midBit = Mid(str, openPos + 1, closePos - openPos - 1)
        If openPos <> 0 And Len(midBit) > 0 Then
        keep = keep & openStr & midBit & closeStr
       End If
       If openPos = 0 Or prevClose = 0 Then
           i = 0
       Else
          i = 1
      End If
      prevOpen = openPos + i
       prevClose = closePos + i
     Wend
     extract_values = keep
     End Function
    

提交回复
热议问题