I am trying to loop through folders using VBA code Found here: Loop Through All Subfolders Using VBA
The code I have simply copied and added my own macro. Although it does not give an error. The code is not working. It simply does not respond when I run the Macro.
I have 500+ files I need to apply a macro to - some of the files are really deeply nested within folders.
I really would appreciate some one to help me create a working Macro. The ones found on stack exchange - either give an error or simply don't respond.
If any one has a macro enabled working version of the code submitted here - it would be a great help.
I am well aware that there are VBA excel looping through folders code, there is also a recursive one - when I test these solutions they do not work for me. That is not to say that they dont work at all.
Pointing me to other threads is not helping me -Ive read all the threads. Ive spent time testing them.
This is What I need:
- Find file of Certain type ie docx in Folder > SubFolder > Sub Folder - Apply my own macro to it
Version 1 Found on Stack exchange:
Sub NewFolder()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = "C:\Users\Shana\Desktop 2\Folder1\"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
ActiveDocument.Range.Text = "Replaced"
' Operate on each file
Next
End Sub
I have looked through the other VBA Loop though folders code. I have been unable to get them to work.
The code when I create macro in the VBA editor simply does not work.
Version 2 Found on stack exchange:
Function GetFilesIn(Folder As String) As Collection
Dim F As String
Set GetFilesIn = New Collection
F = Dir(Folder & "\*")
Do While F <> ""
GetFilesIn.Add F
F = Dir
Loop
End Function
Function GetFoldersIn(Folder As String) As Collection
Dim F As String
Set GetFoldersIn = New Collection
F = Dir(Folder & "\*", vbDirectory)
Do While F <> ""
If GetAttr(Folder & "\" & F) And vbDirectory Then GetFoldersIn.Add F
F = Dir
Loop
End Function
Sub Test()
Dim C As Collection, F
Debug.Print
Debug.Print "Files in C:\"
Set C = GetFilesIn("C:\Users\Shana\Desktop 2\Folder1\")
For Each F In C
Debug.Print F
Next F
Debug.Print
Debug.Print "Folders in C:\"
Set C = GetFoldersIn("C:\Users\Shana\Desktop 2\Folder1\")
For Each F In C
ActiveDocument.Range.Text = "Replaced"
'Debug.Print F
Next F
End Sub
The above also does not work - am I doing something wrong?
This is What I need:
- Find file of Certain type ie docx in Folder > SubFolder > Sub Folder - Apply my own macro to it
Please do not mark as duplicate as I need a working version of VBA code to cycle through all my docx files.