简体   繁体   中英

Importing Data from Text File to Excel

I have a tab-delimited text file called Log.txt that I write to using a macro. Here are the contents of the text file:

"test1  test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1   test1"
"test2  test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2   test2"
"test3  test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3   test3"
"test4  test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4   test4"

I'd like to write each line of the text file (4 lines currently, but will expand with time) to a sheet called "Log" in my spreadsheet, starting at cell B2. Each line has 18 cells worth of data. Here is the code I have so far:

Sub Refresh_Log()
Dim Str As String, FileNum As Integer, FileName As String, Arr() As String
FileNum = FreeFile()
FileName = "P:/Log.txt"
ReDim Arr(17, 0)
Open FileName For Input As #FileNum
While Not EOF(FileNum)
    Line Input #FileNum, Str
    For i = 0 To 17
        Arr(i, UBound(Arr, 2)) = Split(Str, Chr(9))(i)
    Next i
    ReDim Preserve Arr(17, UBound(Arr, 2) + 1)
Wend
Close #FileNum
End Sub

Any tips on how to alter the code to make it write the data from Log.txt into the Log sheet, line by line, starting at cell B2? I don't want it to include the quotation marks at the beginning and end of each line. For example, the first line ("test1...") will go into row 2 starting at column B (B2), second line will go into row 3 starting at column B (B3), third line into row 4 column B, etc.

What you want is write the array to the sheet range like this:

With Sheets("Log")
    .Range("B2").Resize(UBound(Arr, 1) - LBound(Arr, 1) + 1, UBound(Arr, 2) - LBound(Arr, 2) + 1).Value2 = Application.WorksheetFunction.Transpose(Arr)
End With

If you just wanna import the file you can do it this way:

    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & Filename, Destination:=Range("$B$2"))
    .CommandType = 0
    .Name = "log"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 1257
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = True
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM