Consulting

Results 1 to 7 of 7

Thread: Find all values with curly brackets

  1. #1

    Find all values with curly brackets

    I need help revising the code below. What the macro does is extract all values enclosed with TWO curly brackets, e.g. {{anion}}, but it seems to extract only the first value. How can I change this so that it will be able to extract all values enclosed in curly brackets?

    Sub Teststsst()
    
    
    Dim FileNum As Integer
    Dim DataLine As String
    
    
    FileNum = FreeFile()
    Open "C:\Users\XZX\Downloads\BR0613600A2.txt" For Input As #FileNum
    
    
    While Not EOF(FileNum)
        Line Input #FileNum, DataLine
    
    
    openingParen = InStr(DataLine, "{{")
    closingParen = InStr(DataLine, "}}")
    enclosedValue = Mid(DataLine, openingParen + 2, closingParen - openingParen - 2)
    
    
    Debug.Print enclosedValue
    
    
    Wend
    
    
    End Sub

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    Do you mean there could be more than 1 {{ field on the same row?

    dont forget to put CLOSE 1, right before END SUB.

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Try posting an example of what each line is and what you expect to extract. A short simple text file attachment and maybe one showing what you expect would help us help you more.

  4. #4
    a sample would be like this one:
    I have {{two}} hands, the {{left}} and the {{right}}
    this is just a {{sample}}. hope {{anyone}} can help.

    how do i extract two, left, right, sample, and anyone?

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Sub blah()
    myStr = "I have {{two}} hands, the {{left}} and the {{right}} this is just a {{sample}}. hope {{anyone}} can help."
    xxx = Split(myStr, "{{")
    For i = LBound(xxx) To UBound(xxx)
      If InStr(xxx(i), "}}") > 0 Then
        yyy = Split(xxx(i), "}}")
        MsgBox yyy(0)
      End If
    Next i
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb() 
        sn=split(replace(createobject("scripting.filesystemobject").opentextfile("C:\Users\XZX\Downloads\BR0613600A2.txt").readall,"}}","{{"), "{{") 
    
        For j = 1 to ubound(sn)-1 step 2 
          msgbox sn(j) 
        Next
    End Sub

  7. #7
    • Thanks p45cal, snb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •