rjplante
Well-known Member
- Joined
- Oct 31, 2008
- Messages
- 574
- Office Version
- 365
- Platform
- Windows
I have some code I got from here a while ago that uses a subdirectory path to a folder and then locates the next directory based on information in another cell and user will enter a number in. The code is listed below (Search_for_a_folder). I have a macro that opens a userform. The user will enter two numbers (eg 5 and 2405). The first is the number of tabs I need created, and the second is the next piece of information the above mentioned macro needs to locate the next directory. When my user selects the OK button on my userform, the number 2405 is written to the necessary cell for the "Search_for_a_folder" macro to run.
The macro runs and provides the necessary directory as desired, but the code I have that follows the line Call "Search_for_a_folder" will not run. They are simply another two calls one calls my "SAVE_FILE" macro and the other one calls my "Create_Tabs" macro. If I run either of these macros individually, they run perfectly and do exactly what I want them to do. What I am wondering is that there may be something in the "Search_for_a_folder" macro that remains open and in use that prevents any further code from running. Is there some code I can add after the call to terminate any remaining processes from the "Search_for_a_folder" macro? Or better yet, code to add to the end of the "Search_for_a_folder" macro to terminate any running code.
The macro runs and provides the necessary directory as desired, but the code I have that follows the line Call "Search_for_a_folder" will not run. They are simply another two calls one calls my "SAVE_FILE" macro and the other one calls my "Create_Tabs" macro. If I run either of these macros individually, they run perfectly and do exactly what I want them to do. What I am wondering is that there may be something in the "Search_for_a_folder" macro that remains open and in use that prevents any further code from running. Is there some code I can add after the call to terminate any remaining processes from the "Search_for_a_folder" macro? Or better yet, code to add to the end of the "Search_for_a_folder" macro to terminate any running code.
VBA Code:
Option Explicit
Dim xfolders As New Collection
Dim i As Long
Sub Search_for_a_folder()
Dim sPath As String
Sheets("Main Page").Range("AK1").ClearContents
i = 1
Call AddSubDir(Range("AH1").Value)
End Sub
'
Sub AddSubDir(lPath As Variant)
Dim SubDir As New Collection
Dim DirFile As Variant
Dim sd As Variant
On Error GoTo ErrHandler
If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
DirFile = Dir(lPath & "*", vbDirectory)
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If ((GetAttr(lPath & DirFile) And vbDirectory) = 16) Then
If InStr(1, DirFile, Range("C3").Value, vbTextCompare) Then
Range("AK" & i).Value = lPath & DirFile
'i = i + 1
End
End If
SubDir.Add lPath & DirFile
End If
End If
DirFile = Dir
Loop
For Each sd In SubDir
xfolders.Add sd
Call AddSubDir(sd)
Next
ErrHandler:
End Sub