你好,有很多不同的txt文档,里面的数据是测试结果,如下排列,需要导入excel单元格且按行排列

2025-03-28 02:30:07
推荐回答(2个)
回答1:

使用如下的VBA代码可实现批量自动导入数据。

  • 程序会提供选定txt所在文件夹;

  • 然后所有txt文件会导入到excel中;

  • 按照先行后列的方式排列数据,即同一文件按行,不同文件按列排列。


  • Sub 宏1()
    Dim myPath As String
    Dim myFile As String
    Dim i As Single
    i = 1
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    .AllowMultiSelect = False
    myPath = .SelectedItems(1)
    End With
    myFile = Dir(myPath & "\" & "*.txt")
    Do While Len(myFile) <> 0

        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & myPath & "\" & myFile, Destination:=Cells(1, i))
            .Name = "1 - 副本"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 936
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
       myFile = Dir
       i = i + 1
    Loop
    End Sub

回答2:

首先应该把 数据复制到excel 然后通过 公式来达到 但是你所说的条件不够全面 可以把文件发给我 我帮你弄