Sub GetData()
Dim cn As Object
Dim rs As Object
Dim fd As Office.FileDialog
Dim FilePath As String
Dim FileName As String
Dim querystr As String
Sheet2.Cells.ClearContents
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please Select Last Week File."
.Filters.Clear
.Filters.Add "All Files", "*.txt*"
If .Show = True Then
FilePath = .SelectedItems(1)
End If
End With
FileName = Split(FilePath, "\")(UBound(Split(FilePath, "\")))
pth = ThisWorkbook.Path & "\"
cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & pth & ";" & _
"Extended Properties = ""text; HDR=Yes"""
cn.Open cnStr
Application.ScreenUpdating = False
On Error Resume Next
querystr = "SELECT *FROM " & "[" & FileName & "]"
rs.Open querystr, cn, 3, 4
Do While Not rs.EOF
ActiveSheet.Range("A1").CopyFromRecordset rs
Loop
rs.Close
Set rs = Nothing
Set cn = Nothing
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim m As Long
Dim n As Long
n = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
Sheet1.Range("A4:C" & n).ClearContents
Sheet1.[C1] = Sheet2.[A1]
i = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
k = 4
For j = 9 To i
If Range("B9") = vbNullString Then
MsgBox "No Data"
Exit Sub
End If
If Range("B" & j) <> vbNullString Then
Sheet1.Range("A" & k) = Application.WorksheetFunction.Trim(Application.WorksheetFunction.Substitute(Sheet2.Range("A" & j), "---- Session starting", "", 1))
l = Sheet2.Range("A" & j).CurrentRegion.Rows.Count + j - 1
Sheet1.Range("B" & k) = Application.WorksheetFunction.CountA(Range("A" & j + 2 & ":A" & l))
For m = j + 2 To l
Sheet2.Range("D" & m).FormulaR1C1 = _
"=IFERROR(TRIM(MID(RC[-3],FIND(""M 0"",RC[-3])+2,10))+0,TRIM(MID(RC[-3],FIND(""M 0"",RC[-3])+2,10))+0)"
Next
Sheet2.Range("C" & j) = Application.WorksheetFunction.Max(Range("D" & j + 2 & ":D" & l))
Sheet1.Range("C" & k) = Sheet2.Range("C" & j)
k = k + 1
End If
Next
Application.ScreenUpdating = True
End Sub