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

Excel 将*txt导入工作表并用*txt文件名命名,excel,excel-formula,vba,Excel,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 =

我正在尝试制作一个宏,可以从文件夹导入*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
Sub-importText()
作为字符串的Dim strFile
将ws设置为工作表
strFile=Dir(“A:\REPORTS\2017\*.txt”)
执行While strFile vbNullString
设置ws=Sheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“A:\REPORTS\2017\”&strFile,目的地:=范围($A$1))
.Name=strFile
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=65001
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(9,1,1,1,1,1,1,1,1,1,1,1,1,4,4,9)
.TextFileFixedColumnWidths=数组(14,10,6,11,43,15,33,14,1,14,16,4,13,11_
11, 10)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
strFile=Dir
环
端接头

在添加行之后添加一行代码

Set ws = Sheets.Add
ws.Name = strFile 
With ws.QueryTables.Add(...
在“结束于”之后,尝试以下操作:

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

srtFile = Dir
最终代码:

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
Dim strFile作为字符串
将ws设置为工作表
strFile=Dir(“A:\REPORTS\2017\*.txt”)
执行While strFile vbNullString
设置ws=Sheets.Add
ws.Name=strFile
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“A:\REPORTS\2017\”&strFile,目的地:=范围($A$1))
.Name=strFile
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=65001
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(9,1,1,1,1,1,1,1,1,1,1,1,1,4,4,9)
.TextFileFixedColumnWidths=数组(14,10,6,11,43,15,33,14,1,14,16,4,13,11_
11, 10)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
strFile=Dir
环
端接头