looping for files within folder - prevent the code from attempting to open same file

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I have the below code that results in a loop error because the code does not ignore the source file.
Can someone adjust?

Hope I provided enough ...

VBA Code:
Dim fso As FileSystemObject
Dim f As Folder, sf As Folder
Dim ofile As File
Dim lr As Long, LRM As Long
Dim MyPath As String, MyFile As String, File As Workbook
Dim sFolder As String
Dim sFile As String
Dim wbSource As Workbook
Dim wbMaster As Workbook
Dim shTarget As Worksheet
Dim shSource As Worksheet

Application.ScreenUpdating = False

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(ThisWorkbook.Path & "\")

'================================================
' Loop through all files within the same folder
'================================================
For Each f In f
If Not f.Name = "Archive" Or Not f.Name = "ReportWriter.xlsm" Then
For Each ofile In f.Files
If Left(fso.GetExtensionName(ofile.Path), 4) = "xlsm" Then
Set wbMaster = ThisWorkbook
If ofile.Name <> ThisWorkbook.Name Then
                        Set wbSource = Workbooks.Open(ofile)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
It is tough to tell, since you only pasted part of your code.
However, I think you should move this line OUT of your For loop, and put it BEFORE the For loop:
VBA Code:
Set wbMaster = ThisWorkbook
That value should never change, so there is no reason to have that statement within your loop.
And depending on what your other code is doing, if the last file you opened is the active file, it would be resetting wbMaster to that file (and not your original file).
 
Upvote 0
Try this:

VBA Code:
Sub test()
  Dim wbSource As Workbook, wbMaster As Workbook
  Dim shTarget As Worksheet, shSource As Worksheet
  Dim fso As Object, f As Variant, oFile As Variant
  
  Application.ScreenUpdating = False
  
  'On Error Resume Next
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.GetFolder(ThisWorkbook.Path & "\")
  
  '================================================
  ' Loop through all files within the same folder
  '================================================
  For Each oFile In f.Files
    Select Case oFile.Name
      Case "Archive", "ReportWriter.xlsm", ThisWorkbook.Name
      Case Else
        If Left(fso.GetExtensionName(oFile.Path), 4) = "xlsm" And Left(oFile.Name, 1) <> "~" Then
          Set wbMaster = ThisWorkbook
          Set wbSource = Workbooks.Open(oFile)
          '
          'continue your code
          '
        End If
    End Select
  Next oFile
End Sub
 
Upvote 0
Thank you both. It will try both approaches tomorrow. I tried for the last 20 minutes and am getting lost ... Need a fresh start tomorrow.
 
Upvote 0
Joe4,
Thank you for your help. That helped me see and learn something about the code.
After stepping through the code after a good night's sleep, the code was doing exactly what you described.
Much appreciated.


Dante,
Your code is quick, short and amazing. I will use it for this project, and any of my future "looping through all files in a folder" projects.
Thank you so much.
 
Upvote 0
You are welcome.
Glad we could help.
 
Upvote 0

Forum statistics

Threads
1,215,500
Messages
6,125,166
Members
449,210
Latest member
grifaz

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