June
June

Reputation: 1

How to read txt file through excel macros?

I want to export the data from txt file to excel by using excel macros, because I need to analyze 100 txt files per day. However, excel keep failing to read the txt file with the error.

My txt file looks like below. It have two TCD data, and I want excel macros read TCD1 data in first sheet and TCD2 data in second sheet. Txt file have many information. I want macros read the "time" (within the range that I want) and export "area" along with that specific time to excel.

What line should I add or fix?

This is the macros code that I try.

`Sub Import()
'
' Import Macro
'

    Columns("A:H").Select
    Selection.ClearContents
    
    Columns("X").Select
    Selection.ClearContents

' The spaces in dirname between stored. and Example are for looks
    dirname = InputBox("Input the Directory where GC Data is stored.          Example: X:\Data\GC\Backup\DATA\", _
                       "Location 1", "C:\Users\Malek\Desktop\DWF JSS\RUN JSS-GQMULT- 101413 2013-10-28 16-36-31")
   j = InputBox("Input the total number of files to import", "Here we go!", 3)
 
   s = 130
   
Application.ScreenUpdating = False
 Application.Calculation = xlCalculationManual


For i = 1 To j
If i < 10 Then
Filename = "001F0"
ElseIf i < 100 Then
Filename = "001F"
Else
Filename = "001F"
End If
    

N = (s * (i)) + 3

    With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;" & dirname & "\" & Filename & i & "01" & ".D\Report.TXT" _
        , Destination:=Cells(N, 1))
        .Name = "Report"
        .FieldNames = True
        .RowNumbers = 1
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 22
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(5, 8, 6, 7, 11, 11, 9)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
Cells((s * (i)) + 3, 24).Value = i
Next i

i = i - 1
Cells((s * (i)) + 3 + s, 24).Value = "END"

Application.ScreenUpdating = True
 Application.Calculation = xlCalculationAutomatic

'

'
End Sub

And below is the error message that I got.

This is text file that I want to export the data

Upvotes: 0

Views: 85

Answers (0)

Related Questions