How Can I make this macro look in every folder in a folder?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,178
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,

I have this macro that goes to a folder "Patient 47" and converts all the txt to excel files, but I need it to go to all folders not just "Patient 47"
so the fiile path would be: "C:\Users\iplayer\Documents\Odesk New1\41 Odeskrune Hansen mcro\" the every folder in this folder, I don't know what those folders will be called.

is this possible? can someone please show me how to do it?
thanks

Tony

Code:
Sub LoopAllFiles()

    Dim sPath As String, sDir As String
    
    sPath = "C:\Users\iplayer\Documents\Odesk New1\41 Odeskrune Hansen mcro\Patient 47"
    
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    
    sDir = Dir$(sPath & "*.txt", vbNormal)
    Do Until Len(sDir) = 0
        Workbooks.Open (sPath & sDir)
        With ActiveWorkbook
            .SaveAs Filename:=Left(.FullName, InStrRev(.FullName, ".")) & "xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            .Close
        End With
        
        sDir = Dir$
    Loop
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Tony,

I have the following recursive file collection routine hanging around in my toolbox. I don't remember where I got it or who the author was. It did modify it slightly to output the list to the active worksheet. So you should try it in a new workbook or at least on a new blank worksheet. It was originally sending its output to the debug window

I hope it helps.

Gary

In a standard module:
Code:
Option Explicit

Sub dirTest()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer
Dim oCell As Range

startDir = "C:\Users\iplayer\Documents\Odesk New1\41 Odeskrune Hansen mcro\Patient 47"

Call FillDir(startDir, dlist)

Set oCell = Range("A1")
Application.ScreenUpdating = False

For i = 1 To dlist.Count

oCell.Value = dlist(i)
Set oCell = oCell.Offset(1, 0)

Next i

Application.ScreenUpdating = True


End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub
 
Upvote 0
Hi Gary,
Thats great, I'm sure if I tinker with this it will do what I need I didn't know were to start so this is a great help, thanks very much :)
 
Upvote 0
You're very welcome.

I did substitute the "start path" you showed in post #1 in the sample code. As is, it will select every file type. You will also have to change the file type it looks for in the code.

startDir = "C:\Users\iplayer\Documents\Odesk New1\41 Odeskrune Hansen mcro\Patient 47"
 
Upvote 0

Forum statistics

Threads
1,203,203
Messages
6,054,120
Members
444,703
Latest member
pinkyar23

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
Back
Top