public countGlobal as Integer
Sub moFile()
countGlobal = 1
Dim strFileName As Variant
strFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt", _
Title:="Select files", MultiSelect:=True)
readFiles strFileName:=strFileName, count:=1
End Sub
Function readFiles(ByVal strFileName As Variant, ByVal count As Integer) As String
Dim content As String
Dim tenFile As String
Dim oFSO As FileSystemObject
Set oFSO = New FileSystemObject
Dim oFS As TextStream
If count <= UBound(strFileName) Then
Set oFS = oFSO.OpenTextFile(strFileName(count))
content = oFS.ReadAll
count = count + 1
tenFile = Left(GetFilenameFromPath(strFileName(count - 1)), Len(GetFilenameFromPath(strFileName(count - 1))) - 4)
processFile content:=content, lineNum:=0, strFileName:=tenFile
readFiles strFileName:=strFileName, count:=count
End If
readFiles = content
End Function
Function processFile(content As String, lineNum As Integer, strFileName As String)
Dim splitted() As String
Dim sl As Integer
Dim th As String
splitted = Split(content, vbNewLine)
If lineNum <= UBound(splitted) Then
sl = extractSoLuong(splitted(lineNum))
th = extractTenHang2(splitted(lineNum))
Debug.Print countGlobal & "."; strFileName & " - "; sl & " x " & th
With ActiveWorkbook.Sheets(1)
.Cells(countGlobal, 1) = strFileName
.Cells(countGlobal, 2) = th
.Cells(countGlobal, 3) = sl
End With
countGlobal = countGlobal + 1
lineNum = lineNum + 1
processFile content:=content, lineNum:=lineNum, strFileName:=strFileName
End If processFile = sl
End Function
Function extractSoLuong(line)
Dim elem() As String
elem = Split(line, " ")
extractSoLuong = elem(UBound(elem))
End Function
Function extractTenHang(line)
Dim elem() As String
elem = Split(line, " ")
For j = 0 To UBound(elem) - 2
tenHang = tenHang + elem(j) + " "
Next j
extractTenHang = RTrim(tenHang)
End Function
Function extractTenHang2(line)
extractTenHang2 = Left(line, InStrRev(line, "x") - 2)
End Function