VBA 2010 Open files in current folder loop
Solved/Closed
Related:
- VBA 2010 Open files in current folder loop
- Microsoft office 2010 free download - Download - Office suites
- Anvi folder locker reset key - Guide
- Pdf and xps add in 2010 - Download - Other
- How to open msi files on android - Guide
- Vba case like - Guide
1 response
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Aug 7, 2011 at 06:00 PM
Aug 7, 2011 at 06:00 PM
I am not sure what you mean by "next file" What makes a file "a next file" ?
If you next you just meant that process all files in a folder one by one. If you want to do that then you can use the functionality of DIR
If you next you just meant that process all files in a folder one by one. If you want to do that then you can use the functionality of DIR
Public Sub doProcessAllFiles() Dim sThisFilePath As String Dim sFile As String sThisFilePath = ThisWorkbook.Path If (Right(sThisFilePath, 1) <> "\") Then sThisFilePath = sThisFilePath & "\" sFile = Dir(sThisFilePath & "*.xls*") Do While sFile <> vbNullString MsgBox "The next file is " & sFile sFile = Dir Loop End Sub
Aug 7, 2011 at 07:21 PM
I have a personal workbook that I save all my macros to (it was a fix we came up with as the automatic personal macro file wasn't working - something to do with the way our system is setup). I always have that workbook hidden.
When I run the code as you've written it it uses the file path for the Personal Macro book as the file path to find the folders which of course doesn't work. So I tried adapting it changing this bit from:
sThisFilePath = ThisWorkbook.Path to sThisFilePath = ActiveWorkbook.Path
but when it runs even though it identifys correctly with the pop up message what the next file in the folder is that it is going to work on it doesn't seem to open the file and instead runs the macro on the same workbook again.... any advice?
P.S. I've put it together with my code like this
Public Sub doProcessAllFiles()
Dim sThisFilePath As String
Dim sFile As String
sThisFilePath = activeWorkbook.Path
If (Right(sThisFilePath, 1) <> "\") Then sThisFilePath = sThisFilePath & "\"
sFile = Dir(sThisFilePath & "*.xls*")
Do While sFile <> vbNullString
MsgBox "The next file is " & sFile
sFile = Dir
'My own code inserted here
Loop
End Sub
Aug 7, 2011 at 07:56 PM
The code that you modified, is only giving you the name of the file. It is not opening any file. You need to open the file
Dim wbBook As Workbook
Set wbBook = Workbooks.Open(sFile)
Aug 7, 2011 at 08:51 PM
Public Sub doProcessAllFiles()
Dim sThisFilePath As String
Dim sFile As String
Dim wbBook As Workbook
sThisFilePath = activeWorkbook.Path
If (Right(sThisFilePath, 1) <> "\") Then sThisFilePath = sThisFilePath & "\"
sFile = Dir(sThisFilePath & "*.xls*")
Do While sFile <> vbNullString
sFile = Dir
'My own code inserted here
Set wbBook = Workbooks.Open(sFile)
Loop
End Sub
It's coming up with Run Time error '1004'. In the message it includes the name of the file it's looking for (which is the right one) but says it can't find it....?
Aug 8, 2011 at 05:49 AM
Aug 8, 2011 at 04:45 PM
ChDir (sThisFilePath)
It comes up with Error 76 - Path not found....?