excel vba- extract text between 2 characters

前端 未结 4 866
隐瞒了意图╮
隐瞒了意图╮ 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 16:02

    If I have a cucumber table as test data in a cell, and need to access the value under 'Header 1' between 4th and 5th pipes, below is how I done it. My table example as below in cell D7 :

    *Cell D7:

    code to access 'abcd' which is found after 4th occurrence of '|' and before 5th occurrence of '|'

    Dim sheet5 As Worksheet
    Dim i As Integer
    Dim k As Integer
    Dim openPos As Long
    Dim clsPos As Long
    Dim textBetween as String
    
    'Using for loop to find 4th occurrence of pipe '|' for openPos
    For i = 1 To 4
    openPos = InStr(openPos + 1, sheet5.Range("D7"), "|", vbTextCompare)
    Next i
    
    'Using for loop to find 5th occurrence of pipe '|' for clsPos
    For k = 1 To 5
    clsPos = InStr(clsPos + 1, sheet5.Range("D7"), "|", vbTextCompare)
    Next k
    
    'Displaying the value between openPos and clsPos
    txtBetween = Mid(sheet5.Range("D7").Value, openPos + 1, clsPos - openPos - 1)
    MsgBox ("Current Header 1 value: " & txtBetween)
    

提交回复
热议问题