Import the text file into Excel data table

% R & lt Dim
Sub ImportFromTextFile ()
Dim FSO of As Object, Object sFile of As, blnExist of As Boolean
Dim FileName of As String, Integer I of As, iCol of As Integer, String LineText of As
Const. 1 = ForReading
the Set FSO = the CreateObject ( "Scripting.FileSystemObject") 'create FileSystemObject object
fileName = "H: \ nama_exi_p.tsv" ' specified text file name
blnExist = fso.FileExists (fileName) 'to determine whether a file exists, and if not, then the exit procedure
If not blnExist then MsgBox "file does not exist! ": Exit Sub
the Set sFile = fso.OpenTextFile (FileName, ForReading) 'create and open TextStream object named sFile the
Do While not sFile.AtEndOfStream' if not the end of the text file, and then read the data
LineText = sFile.ReadLine
Call getNum1 (LineText)
Loop
sFile.Close
Set fso = Nothing
Set sFile = Nothing
End Sub
Sub getNum1(str As String)
r = r + 1
Set reg = CreateObject("VBScript.RegExp")
With reg
.Global = True
.IgnoreCase = True
.Pattern = "(\S+)\s+(\S+)\s+(\S+)"
End With

Set mc = reg.Execute(str)
For Each m In mc

Cells(r, 1) = m.submatches.Item(0)
Cells(r, 2) = m.submatches.Item(1)
Cells(r, 3) = m.submatches.Item(2)
Next
End Sub

Guess you like

Origin www.cnblogs.com/zhujie-com/p/11932911.html