VBA Problem. Looping Through Files in Folder.

hellfire45

Active Member
Joined
Jun 7, 2014
Messages
464
Hi guys, Ya'll are usually really good at the puzzly trouble shooting and I'm looking for some assistance.

My goal is to get this to loop through the folder at the path ultimately stored in STRFOLDER but it keeps opening and closing the same excel file in the folder instead of looping through the 70 or so files in the folder. Any idea of how to get it to move onto the next file in the folder until it has hit every file? Thanks guys, I'm sure its something really small but I just can't quite figure it out.

VBA Code:
upath = Application.ActiveWorkbook.Path

i = 0
Do
srcpath = upath & "\" & Format(Date - i, "yyyy mm dd")
                If Dir(srcpath & "\", vbDirectory) <> "" Then
                                Exit Do
                Else
                                i = i + 1
                End If
Loop Until i = 35


FolderNames = Array("AM\", "VP\")
For Each Folder In FolderNames
            STRFOLDER = srcpath & "\" & Folder
            strFileSpec = STRFOLDER & "*.*"
            Set fso = CreateObject("Scripting.FileSystemObject")
            
            File_iteration = 0
            err_count = 0
            file_counter.CountFiles
            
            For Each FileInFolder In fso.getfolder(STRFOLDER).Files
                        On Error GoTo nextfile:
                        File_iteration = File_iteration + 1 'keeps track of the iteration of files being processed
                        Application.StatusBar = File_iteration - 1 & " of " & j & " Files Processed. Please be Patient."
            
                        strFileName = Dir(strFileSpec)
                        sFile = Dir(STRFOLDER & strFileName)
                        If Folder = "AM\" Then
                                    Workbooks.Open STRFOLDER & sFile, ReadOnly:=True
                                    sheet_num = ActiveWorkbook.Sheets.Count
                                    For x_sheet = 2 To sheet_num
                                                Set wscs = ActiveWorkbook.Sheets(x_sheet)
'a whole bunch of worksheet processes stuff

             next
                                    Next
                                    ActiveWorkbook.Close False
                        End If
loopsetter:
            Next FileInFolder  'next iteration in loop
Next Folder
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I don't really want to try to untangle your code, but here is an example of how to use the FileSystemObject to loop though all files in a folder.
VBA Code:
Sub FileLoop()
    Dim FSO As Object
    Dim FileObj As Object
    Dim FolderPath As String

    FolderPath = "C:\Windows"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FolderExists(FolderPath) Then
        For Each FileObj In FSO.GetFolder(FolderPath).Files
            Debug.Print FileObj.Name                     'list files
            '
            'Do stuff here
            '
        Next FileObj
    Else
        MsgBox FolderPath & " not found"
    End If
End Sub
 
Upvote 0
Hi Thanks for the reply. I'm definitely capable of missing lots of things in this coding universe but unless I am, I think you suggested what I was already doing. Which, if so, has me extra perplexed.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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