如何将多个.TXT文本文件合并到一个excel表中

如何将多个.TXT文本文件合并到一个excel表中

将要合并的多个.TXT文本置于同一个文件夹中,并新建excel表格。在excel中输入以下代码:

Sub all_contents()

Application.ScreenUpdating = False

P = ActiveWorkbook.Path & "/"

N = 1

For Each File In CreateObject("Scripting.FilesyStemObject").GetFolder(P).Files

If File.Name Like "*.TXT" Then

S = CreateObject("Scripting.FileSystemObject").OpenTextFile(P & File.Name, 1, False).ReadAll()

T = Split(S, vbLf)

Cells(N, 1).Resize(UBound(T) + 1, 1) = Application.Transpose(T)

End If

N = N + UBound(T)

Next

Application.ScreenUpdating = True

MsgBox "恭喜大人,已成功合并文本。"

End Sub

    原文作者:蒙奇奇的蒙
    原文地址: https://blog.csdn.net/weixin_43677714/article/details/105101591
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞