VBA导入多个带有名称的txt文件

时间:2015-08-27 15:51:57

标签: excel vba text

我使用此代码将txt文件导入我的工作簿,也导入txt文件的名称。它只导入9个txt文件,我收到消息“完成”。我检查了txt文件 - 一切都很好。有什么想法或建议吗?

Sub loadfiles()

    Dim fpath As String
    Dim fname As String


    fpath = "C:\...\data\"
    fname = Dir(fpath & "*.txt")
    For i = 1 To 200
     Application.StatusBar = "Progress: " & i & " of 100"
        Sheet1.Range("B1" & i).Value = fname
        Sheet1.Select
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
          & fpath & fname, Destination:=Range("A1" & i))
            .Name = "a"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
            fname = Dir
        End With
    Next i
    Application.StatusBar = False
    MsgBox "Done"
End Sub

0 个答案:

没有答案