Code following macro doesn't run

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. 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.

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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I figured it out. I needed to change the End in the middle to Exit Do and now it all works. Thanks for indulging me on the board.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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