【问题标题】:importing multiple text files into separate Excel sheets in 1 Excel book using VBA使用 VBA 将多个文本文件导入 1 个 Excel 书中的单独 Excel 工作表
【发布时间】:2014-01-29 16:38:48
【问题描述】:

我编写了一个 VBA 宏,用于将许多文本文件(来自 1 个文件夹)导入到 1 个 Excel 工作簿中的单独工作表中。所有文件都读入每个单独的工作表就好了。但是,我看到了字段放置问题。每个文本文件的标题都是相同的。但是,字段值本身有时会被一些字段压低。因此,并非所有字段值都在其正确的标题下排列。有人可以向我建议为什么会这样吗?我试过看看它是制表符分隔的还是竖线分隔的问题,但这似乎不是问题。

Sub MultipleTextFilesIntoExcelSheets()
    Dim i As Integer 'a counter to loop through the files in the folder
    Dim fname As String, FullName As String 'fname is the name of the file, and FullName is the name of its path
    Dim ws As Worksheet 'a workbook object for the workbook where the current macro is running

i = 0 'seed the counter

'get the name of the first text file
fname = Dir("C:\dummy_path\*txt")

'loop through the text files to put them onto separate sheets in the Excel book
While (Len(fname) > 0)
    'get the full path of the text file
    FullName = "C:\dummy_path\" & fname
    i = i + 1 'get ready for the next iteration

    Set ws = ThisWorkbook.Sheets("Sheet" & i) 'the current sheet

    With ws.QueryTables.Add(Connection:="TEXT;" & FullName, Destination:=ws.Range("A1"))
        .Name = "a" & i
        .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 = True    'we are using a tab-delimited file
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        fname = Dir
    End With
Wend

结束子

【问题讨论】:

  • VBA 将字母“f”视为制表符分隔符。有人知道为什么或如何解决它吗?
  • 知道为什么我在运行此代码时会收到“下标超出范围”错误吗?它确实将第一个 txt 文件导入到与我的宏按钮相同的工作表中,然后在这一行出现错误 Set ws = ThisWorkbook.Sheets("Sheet" & i)
  • 刚刚发现这段代码非常好用,并用txt文件名label-sheets-while-importing-multiple-text-files-excel-vba标记每张纸

标签: excel text import vba


【解决方案1】:

“把 F 当作分隔符”的问题是因为这行:

.TextFileOtherDelimiter = False

删除它会使 VBA 按预期工作。我认为 TextFileOtherDelimiter 的默认值应该是“null”而不是 False。

【讨论】:

    【解决方案2】:

    .ConsecutiveDelimiter = False 更改为.ConsecutiveDelimiter = True

    一些细节:这可能是因为可能有多个制表符分隔“列”。更改此参数将允许接受多个选项卡作为一个选项卡。

    【讨论】:

    • 这样做仍然会产生相同的效果,但方向相反。我的某些字段不一定填充在我的文本文件中。因此,通过将连续分隔符设置为 True,它将这些空字段之后的字段值向下推送到错误的字段中。一定还有什么我需要改变的。
    • 我也遇到过这种问题,我想解决这个问题的唯一方法是使用其他定界工具,即固定宽度定界符。这将允许您每隔 x、y、z 等字符“剪切一个新列”。
    • 即便如此,它也可能无法解决您的问题。想到的另一个解决方案是实现一个“更正”宏,该宏将根据预定义的规则插入/删除空格。
    • 感谢两位的建议。当我手动导入每个文本文件时(通过 Excel 的文本文件导入向导),一切看起来都很好;所有字段值都在其正确的列中排列。所以,一定有一些向导内置的东西我没有在这个宏中复制。
    • 我刚刚注意到一个模式。每当宏遇到字母“f”时,它会将其后面的所有文本推入下一个字段(并且不打印“f”)。这很奇怪。
    猜你喜欢
    • 2016-04-17
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多