Problem with possible macro excel vb [closed]

0

I am working with lots of txt files and I need a macro excel to do the following (if possible).

I detail it: I have a path with different .txt with different names, this is an example

  

prueba1.txt

     

prueba2.txt

     

prueba3.txt

     

prueba4.txt

     

prueba5.txt

I need a macro to export the name of each txt file to excel and open each file and read its lines and put the number of lines by txt something like this:

  

NAME FILE TXT ///// LINES

     

prueba1.txt //// 5

     

prueba2.txt //// 4000

     

prueba3.txt //// 22541

     

prueba4.txt //// 19874

     

prueba5.txt //// 44457

Someone to help me get this out?

    
asked by Sergio 08.09.2016 в 09:50
source

1 answer

2

The code for this is the following. Thanks to the forum Zero Racing Forumcoches

Sub ShowFolderList()
Dim h As Worksheet
Set h = Sheets("Hoja1")
    Dim fs, f, f1, fc, s, sFldr, folderspec
    folderspec = "C:\carpeta\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    Set fc = f.Files
    x = 1
    For Each f1 In fc
        f2 = Replace(f1, folderspec, "")
        Set theFile = fso.OpenTextFile(f1, 8, True)
        h.Cells(x, 1).Value = f2 & "\\" & theFile.Line
        x = x + 1
    Next
End Sub
    
answered by 08.09.2016 в 12:20