【问题标题】:Counting the lines of several txt files [closed]计算几个txt文件的行数[关闭]
【发布时间】:2018-08-10 00:27:46
【问题描述】:

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

我想在 excel 中使用 VBA 宏,但我没有足够的技能。

基本流程是: 1.打开x文件txt 2.在excel中的一个单元格中记录文件的标题,在另一个单元格中记录os行/记录的数量。 例如-> 包含 100 条记录和标题 TEST 的 txt 文件应该出现: A1 A2 标题 100

【问题讨论】:

标签: vba excel


【解决方案1】:

试试这个代码

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

【讨论】:

  • 看起来很棒!谢谢,但我不能选择txt文件。您能否更改代码以从一个文件夹而不是文件打开所有文件?
  • 代码已经使您能够选择包含文本文件的文件夹..
  • 好的,很好,但不允许我这样做。 “Set pth = fso.OpenTextFile(strFile, 1)”中的错误调试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多