PDA

View Full Version : Find all values with curly brackets



swaggerbox
10-03-2014, 02:47 AM
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

ranman256
10-03-2014, 05:33 AM
Do you mean there could be more than 1 {{ field on the same row?

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

Kenneth Hobs
10-03-2014, 06:04 AM
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.

swaggerbox
10-03-2014, 08:02 PM
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?

p45cal
10-04-2014, 07:29 AM
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

snb
10-04-2014, 08:02 AM
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

swaggerbox
10-04-2014, 10:21 PM
Thanks p45cal, snb