将* txt导入工作表并使用* txt文件名

时间:2017-07-26 11:48:23

标签: excel excel-vba excel-formula vba

我正在尝试创建一个可以从文件夹中导入* txt文件的宏,我做到了。现在我坚持这个:

我需要将工作表命名为* txt文件。实际代码将作为新工作表的默认名称导入。

Sub ImportTXT()

    Dim strFile As String
    Dim ws As Worksheet
    strFile = Dir("A:\REPORTS\2017\*.txt")
    Do While strFile <> vbNullString
    Set ws = Sheets.Add
    With ws.QueryTables.Add(Connection:= _
        "TEXT;" & "A:\REPORTS\2017\" & strFile, Destination:=Range("$A$1"))
            .Name = strFile
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 65001
            .TextFileStartRow = 1
            .TextFileParseType = xlFixedWidth
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 9)
            .TextFileFixedColumnWidths = Array(14, 10, 6, 11, 43, 15, 33, 14, 1, 14, 16, 4, 13, 11, _
            11, 10)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    End With
    strFile = Dir
    Loop
End Sub

3 个答案:

答案 0 :(得分:1)

在添加行

后添加一行代码
Set ws = Sheets.Add
ws.Name = strFile 
With ws.QueryTables.Add(...

答案 1 :(得分:0)

&#34;结束&#34;,试试:

Set ws = ThisWorkbook.ActiveSheet
ws.Name = Left(srtFile, Len(srtFile) - Len(".txt"))

srtFile = Dir

答案 2 :(得分:0)

最终代码:

Dim strFile As String
Dim ws As Worksheet
strFile = Dir("A:\REPORTS\2017\*.txt")
Do While strFile <> vbNullString
Set ws = Sheets.Add
ws.Name = strFile
With ws.QueryTables.Add(Connection:= _
    "TEXT;" & "A:\REPORTS\2017\" & strFile, Destination:=Range("$A$1"))
        .Name = strFile
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 65001
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 9)
        .TextFileFixedColumnWidths = Array(14, 10, 6, 11, 43, 15, 33, 14, 1, 14, 16, 4, 13, 11, _
        11, 10)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
End With
strFile = Dir
Loop
End Sub