批量txt文件一键合并(VBA 实现)

本案例可将指定路径下所有txt格式文件合并为一个txt格式总文件,开发环境为vba,代码如下:(用户需将路径改为txt文件所在路径)

Sub union_txt_file()
Dim smallfile As String, resultfile As String
Dim a As String, b As String, path As String
path = "E:\练习"
mytime = Format(Now, "yymmdd_hh_mm_ss")
resultfile = "结果" & mytime & "_.txt"
resultfile_path = path & "\" & resultfile
smallfile = Dir(path & "\*.txt")
smallfile_fullname = path & "\" & smallfile
Open resultfile_path For Output As #1
Do While smallfile <> ""
    If smallfile <> resultfile Then
    Open smallfile_fullname For Input As #2
        Do While Not EOF(2)
            Line Input #2, a
            b = b & a
        Loop
        Print #1, smallfile & vbCrLf
        Print #1, b & vbCrLf
        Close #2
     End If
   smallfile = Dir
Loop
 Close #1
 MsgBox "合并已完成" & Space(50) & vbCr & _
 "vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值