Open files in folder and sub folder and copy across data.

homegrownandy

New Member
Joined
Jun 15, 2015
Messages
7
Code:
Sub Test()
     
    Dim strFiles As Variant
    Dim lngLoop As Long
    Dim lngRow As Long
     
    Dim wb As Excel.Workbook
     
    On Error GoTo Catch
    Application.ScreenUpdating = False
     
     '// Change directory as needed
    strfile = Split(TFILES("C:\Users\Admin\Documents\test", True, "*.XLSX"), vbCrLf)

    For lngLoop = 0 To UBound(strfile)
         
         '// TFiles seems to return an additional blank element at the end.
        If strfile(lngLoop) <> vbNullString Then
             
            Set wb = Workbooks.Open(strfile(lngLoop))
             '// Assuming everything goes to the sheet named. Sheet1. Used any old cell references. Will need to change
            lngRow = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
             
            ThisWorkbook.Sheets("Sheet1").Range("A" & lngRow).Value = wb.ActiveSheet.Range("C4").Value
            ThisWorkbook.Sheets("Sheet1").Range("B" & lngRow).Value = wb.ActiveSheet.Range("D4").Value
            ThisWorkbook.Sheets("Sheet1").Range("C" & lngRow).Value = wb.ActiveSheet.Range("E4").Value
             
            wb.Close SaveChanges:=xlDoNotSaveChanges
            Set wb = Nothing
        End If
         
    Next
     
Catch:
     
    Application.ScreenUpdating = True
     
     
End Sub
 
 
Private Function TFILES(parentFolder As String, searchSubfolders As Boolean, _
    Optional searchParams As String = "*.*") As String
     
    vSub = ""
    If searchSubfolders = True Then vSub = "/S "
     
    If Right(parentFolder, 1) <> "\" Then parentFolder = parentFolder & "\"
     
    TFILES = CreateObject("WScript.Shell").Exec("CMD.exe /S /C DIR " & Chr(34) & _
    parentFolder & searchParams & Chr(34) & Space(1) & vSub & "/B").StdOut.ReadAll
     
End Function

I'm using the code above (from a different forum) to search folders and sub folders for xlsx files, and then display results on the workbook I run it from,

However I cant get this to work, Is there any obvious mistakes I've made?

There is no error message so I'm assuming its all running okay its just the fact I get no results.

Thanks in advance!

Andrew.
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Watch MrExcel Video

Forum statistics

Threads
1,123,156
Messages
5,600,027
Members
414,356
Latest member
death20

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top