Looping through all files in a directory, but open file does not loop

shelim481

New Member
Joined
Aug 30, 2018
Messages
28
Please help, my issue is that the code below works but not for active work book, so when i run the macro from the a open workbook in the same directory, it works for all the files but not for the one thats opened and im running from...

i want it to run for the file thats open too.


Code:
Allfolders = MsgBox("Do you want to Run for All files in this folder", vbYesNo)
    If Allfolders = vbYes Then
	
    If Right(MyPath, 1) <> Application.PathSeparator Then MyPath = MyPath & Application.PathSeparator
    Fname = Dir(MyPath & "*.xlsm")


    'loop through the files
    Do While Len(Fname)


        With Workbooks.Open(MyPath & Fname)
        
           ' code here
End With
            xFileName = Dir
        Loop
    End If
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This line
Code:
 xFileName = Dir
should be
Code:
 Fname = Dir
What is MyPath?
 
Upvote 0
Code:
Dim MyPath As String
Dim Allfolders As String
Dim Fname As String

MyPath = Application.ThisWorkbook.Path & "\"
 
Upvote 0
i changed that and still does not work JUST for the file that im running the macro from in the same directory as all other files
 
Last edited:
Upvote 0
With MyPath like that, it will only open files that are in the same folder as the file containing the macro.
Is that not what you want?
 
Upvote 0
I want the file that's already open to run the macro, as currently all the other files run the macro but the one I'm running the macro from..
 
Upvote 0
I'm afraid that I don't understand what you're saying.
Your code will look in the folder that the workbook containing the code is in. Is that what you want?
 
Upvote 0
Yes that's what I want...but because the file I'm running the macro from is open, it does not run the macro on the open file..on all the other file it works..
 
Upvote 0
Ok, I think I understand, what your saying, but I don't understand why.
As well as having the code to process the files inside the loop, you will need to add it after the loop has finished, pointing at ThsWorkbook
 
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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