计算几个txt文件的行[关闭]

问题描述 投票:-7回答:1

我需要计算几个txt文件的行(记录)并在excel上记录。你能帮我找到最好的方法吗?

我在excel的VBA宏上想过但是我没有足够的技能。

基本程序是:1。打开x文件txt 2.在一个单元格中的excel上记录文件的标题,在其他单元格中记录数字os行/记录。例如 - >具有100条记录的txt文件和标题TEST应该出现:A1 A2标题100

vba excel-vba excel
1个回答
0
投票

试试这个代码

Sub Loop_Through_Text_Files_Count_Lines()
Dim fso         As Object
Dim pth         As Object
Dim strFolder   As String
Dim strFile     As String
Dim r           As Long

With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show Then strFolder = .SelectedItems(1) & "\" Else Exit Sub
End With

Set fso = CreateObject("Scripting.FileSystemObject")
strFile = Dir(strFolder & "*.txt")

Do While strFile <> ""
    r = r + 1
    Set pth = fso.OpenTextFile(strFile, 1)
    pth.ReadAll

    Cells(r, 1).Value = strFile
    Cells(r, 2).Value = pth.Line

    strFile = Dir
Loop
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.