I have a txt file including some comments lines and a lot of data lines like below
XYZ3-CCAV::[2] mcb XYZ3 hpy diag ce56 dsc [UT000029118.494] XYZ3:mcb >> LN (CDRxN , UC_CFG,XTP_RST,STP) SD LCK XRMPP CLK90 CLKP1 PF(M,L) VGA DCO P1kII M1kII EPD(1,2,3,4,5,6) XTMPP AMAP(n1,m,p1,2,3,rpara) Head(L,R,U,D) LINK_TIME [UT000029118.495] XYZ3:mcb >> 0 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 44 2 0,1 17 4 205 0 30, 2, 2, -2, 1, 1 0 22, 90, 0, 0, 0, 0 296,464,153,155 57.6 [UT000029118.495] XYZ3:mcb >> 1 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 44 0 0,1 17 2 202 0 31, 2, -1, 5, -1, 1 0 22, 90, 0, 0, 0, 0 296,464,155,155 58.5 [UT000029118.496] XYZ3:mcb >> 2 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 43 0 0,1 17 0 209 0 33, 1, 0, 1, 3, -3 0 22, 90, 0, 0, 0, 0 312,449,159,159 60.1 [UT000029118.497] XYZ3:mcb >> 3 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 1 45 0 0,1 17 6 202 0 33, 2, 0, -1, 3, 0 0 22, 90, 0, 0, 0, 0 328,449,153,159 60.3 [UT000029118.497] XYZ3:mcb >> XYZ3-CCAV::[2] Headscan 51 0 0xf 0 Headscan: min_dwell_bits 100000 Headscan: max_dwell_bits 100000000
I can use Excel built-in regular expression (VBS) to extract the data line
[UT000029118.495] XYZ3:mcb >> 0 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 44 2 0,1 17 4 205 0 30, 2, 2, -2, 1, 1 0 22, 90, 0, 0, 0, 0 296,464,153,155 57.6 [UT000029118.495] XYZ3:mcb >> 1 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 44 0 0,1 17 2 202 0 31, 2, -1, 5, -1, 1 0 22, 90, 0, 0, 0, 0 296,464,155,155 58.5 [UT000029118.496] XYZ3:mcb >> 2 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 0 43 0 0,1 17 0 209 0 33, 1, 0, 1, 3, -3 0 22, 90, 0, 0, 0, 0 312,449,159,159 60.1 [UT000029118.497] XYZ3:mcb >> 3 (OSx1:x1, 0x0c, 0,0, 0) 1* 1* 1 45 0 0,1 17 6 202 0 33, 2, 0, -1, 3, 0 0 22, 90, 0, 0, 0, 0 328,449,153,159 60.3
I tried to write the data lines into an Excel file using below code (A sheet named "EyeInfo" was created in an Excel file):
Sub open_log_file()
Dim Full_Name As String, text As String, textline As String
Dim ws As Worksheet 'Used to Store file path and file name
'Set up worksheet
Set ws = Worksheets("EyeInfo")
ws.UsedRange.Clear
'Call the Window to open the file
Full_Name = Application.GetOpenFilename("Diag Log File(*.log;*.txt;*.*),*.log;*.txt;*.*")
'read the file
Open Full_Name For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
' define regular expression
Dim regEx_CE As Object
Set regEx_CE = CreateObject("VBScript.RegExp")
With regEx_CE
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "\w*\[\d+\]\s+mcb\s+XYZ3\s+hpy\s+diag\s+(ce\d+)\s+dsc"
End With
Dim regEx_LN As Object
Set regEx_LN = CreateObject("VBScript.RegExp")
With regEx_LN
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "\[\w*\.\w*\]\s*\w*:\w*\s*>>\s*\d+.*"
End With
' Execute the match process line by line and put the data in Excel/EyeInfo
Set CE_match = regEx_CE.Execute(text)
Set LN_match = regEx_LN.Execute(text)
ws.Cells(1, 1) = Full_Name
ws.Cells(2, 1) = "Number of Ports to Be Extracted"
ws.Cells(2, 2) = CE_match.Count
For i = 0 To CE_match.Count - 1
ws.Cells(i * 4 + 3, 1) = CE_match(i).Value
ws.Cells(i * 4 + 3, 2) = LN_match(i * 4 + 0).Value
ws.Cells(i * 4 + 4, 2) = LN_match(i * 4 + 1).Value
ws.Cells(i * 4 + 5, 2) = LN_match(i * 4 + 2).Value
ws.Cells(i * 4 + 6, 2) = LN_match(i * 4 + 3).Value
Next
End Sub
What I wanted to do is to put the data in a row delimited by space or comma, so that each data in the data line can be well put in each cell of the row. But this code puts the whole data line in a single cell in Excel.


CE_match(i)into a single cell in columnAof your worksheet. Since you choose not to share your code, nor even tell us whether you needVBAorVBSCRIPTcode, I suspect your question will be closed.