You need to enable JavaScript to run this app. Excel 2016:用VBA批量导入两个每日覆盖的制表符分隔文本文件
优惠活动
大模型
产品
解决方案
定价
更多

Excel 2016:用VBA批量导入两个每日覆盖的制表符分隔文本文件

解决Excel 2016中导入两个制表符分隔文本文件并保留格式的问题

Hey there! Let's fix this for you. You mentioned that manually importing a single file with the recorded QueryTables code works perfectly for keeping formatting, but the FileSystemObject approach you tried didn't do the trick. That makes sense—FSO just reads raw text and splits it, missing all the formatting preservation and parsing rules your recorded code uses.

We can build on your working recorded code to handle both files cleanly, ensuring each imports without overwriting the other and retains all your desired formatting.

最终VBA代码

Sub ImportTwoTextFiles()
    Dim filePath1 As String
    Dim filePath2 As String
    Dim lastRow As Long
    
    ' 设置两个文本文件的路径,请根据实际路径修改
    filePath1 = "C:\Users\Mr D\Music\New folder\A.txt"
    filePath2 = "C:\Users\Mr D\Music\New folder\B.txt"
    
    ' 导入第一个文件到A1起始位置
    ImportTabDelimitedFile filePath1, ActiveSheet.Range("A1")
    
    ' 找到第一个文件数据的最后一行,第二个文件从下一行开始导入
    lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row + 1
    ImportTabDelimitedFile filePath2, ActiveSheet.Range("A" & lastRow)
End Sub

' 通用导入函数,复用你录制的QueryTables逻辑
Sub ImportTabDelimitedFile(filePath As String, destRange As Range)
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & filePath, Destination:=destRange)
        .CommandType = 0
        .Name = Split(filePath, "\")(UBound(Split(filePath, "\"))) ' 用文件名作为QueryTable名称
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True ' 保留原有格式
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True ' 制表符分隔
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1) ' 所有列设为常规格式,可根据需要调整
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

代码说明

  • 通用函数封装:把你录制的有效代码放进ImportTabDelimitedFile函数,这样可以重复调用,避免冗余代码,也方便后续修改格式规则。
  • 避免覆盖:导入第二个文件前,先找到第一个文件数据的最后一行,从下一行开始导入,确保两个文件的数据不会重叠。
  • 保留格式:保留了你录制代码中的PreserveFormatting = True和AdjustColumnWidth = True等关键设置,和手动导入效果一致。
  • 灵活路径:开头的filePath1和filePath2可以根据你的实际文件夹路径修改,确保指向正确的两个文本文件。

使用步骤

  1. 打开Excel 2016,按下Alt + F11打开VBA编辑器。
  2. 插入一个新模块(右键点击工作簿名称 → 插入 → 模块)。
  3. 将上面的代码粘贴到模块中。
  4. 修改filePath1和filePath2为你的两个文本文件的实际路径。
  5. 回到Excel,按下Alt + F8,选择ImportTwoTextFiles并执行。

这样就能每天轻松导入两个被覆盖的文本文件,完美保留你需要的格式啦!

内容的提问来源于stack exchange,提问作者Dani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:37:40