+ Reply to Thread
Results 1 to 23 of 23

[help] macro to open several folders

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Red face [help] macro to open several folders

    Hi everyone, I need a macro that Open a speciefic directory that has several subfolders in it, open each subfolder, open all files, run a speciefic macro that I have, close the file, leave and do it on the next subfolder.

    It's something like this:

    > open speciefic directory "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO"
    > open first subfolder in "PROJETO X1"
    > open first file in folder "PROJETO - FLUXO - BRA.xlsm"
    > run speciefic macro AJUS
    > close file "PROJETO - FLUXO - BRA.xlsm"
    > Loop on all files in subfolder "PROJETO X1"
    > Close subfolder
    > open next subfolder...
    < Loop on all subfolder in directory "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO"

    That will help me so much!
    I'd appreciate it a lot.

    Thank you!

  2. #2
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Goes Up!

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

    Sub OpenSubfoldersFileUpdate() 
    Dim strFile As String
       Dim objFSO, destRow As Long
       Dim mainFolder, mySubFolder
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       mFolder = "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO\"
       Set mainFolder = objFSO.GetFolder(mFolder)
       For Each mySubFolder In mainFolder.subfolders
         strFile = Dir(mySubFolder & "\*.xls*")
         Do While strFile <> ""
            Workbooks.Open mySubFolder & "\" & strFile
            MsgBox strFile
    ' <<<<<<<<< put your code
            ActiveWorkbook.Close True
            strFile = Dir
         Loop
       Next
    End Sub
    If solved remember to mark Thread as solved

  4. #4
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Hello!

    It works perfectly!!

    I would like one more thing, is there a way to close the last file opened before openning the next one?

    Thank you!!!
    Best regards

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

         Do While strFile <> ""
            Workbooks.Open mySubFolder & "\" & strFile
            MsgBox strFile
    ' <<<<<<<<< put your code
            ActiveWorkbook.Close True ' close opened workbook 
            strFile = Dir
         Loop

  6. #6
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Yeah, I saw that!
    But the problem is: when I copy the data from the spread sheet opened, I paste it into a "mother spread sheet" and if I do "ActiveWorkbook.Close True", it will close my active window ("mother spread sheet") and not the file just opened.

    Is there a way to do that?

    Thanks!!

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

    the opened workbook must remain active, don't select the main
    Sub OpenSubfoldersFileUpdate() 
    Dim strFile As String
       Dim objFSO, destRow As Long
       Dim mainFolder, mySubFolder
       set wbmain = thisworkbook
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       mFolder = "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO\"
       Set mainFolder = objFSO.GetFolder(mFolder)
       For Each mySubFolder In mainFolder.subfolders
         strFile = Dir(mySubFolder & "\*.xls*")
         Do While strFile <> ""
            Workbooks.Open mySubFolder & "\" & strFile
            MsgBox strFile
    ' <<<<<<<<< put your code
            range("range to copy").copy wbmain.range("pasting range")
            ActiveWorkbook.Close True
            strFile = Dir
         Loop
       Next
    End Sub
    if problems paste here your final code
    Last edited by patel45; 10-17-2013 at 01:42 AM.

  8. #8
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Dim strFile As String
        Dim objFSO, destRow As Long
        Dim mainFolder, mySubFolder
        Set wbmain = ThisWorkbook
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        mFolder = "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO\"
        Set mainFolder = objFSO.GetFolder(mFolder)
        For Each mySubFolder In mainFolder.subfolders
        strFile = Dir(mySubFolder & "\*.xls*")
            Do While strFile <> ""
                Workbooks.Open mySubFolder & "\" & strFile, ReadOnly:=True
            AJUSTEC
            REPETE
            
        Range("B7:AD7").Select
        Range(Selection, Selection.End(xlDown)).Copy
        wbmain.Range
        
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Selection.End(xlUp).Select
        Selection.End(xlDown).Offset(1, 0).Select
            
            Application.DisplayAlerts = False
            ActiveWindow.Close
            strFile = Dir
         Loop
       Next

    It doesn't work...
    Last edited by arlu1201; 10-17-2013 at 07:46 AM.

  9. #9
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

    use this line for opening files
    set wb1 = Workbooks.Open mySubFolder & "\" & strFile, ReadOnly:=True
    then use wb1 for referencing ranges and closing opened workbook

  10. #10
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    I'm sorry I didn't understand...could you exemplify?

    Thanks a lot!

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: [help] macro to open several folders

    Gila,

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE] before your code and [/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  12. #12
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

    you did not post the whole final code, you have to post the subs AJUSTEC and REPETE

  13. #13
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Smile Re: [help] macro to open several folders

    Sub REPETE()
        Sheets("DADOS DA VENDA").Select
        Range("A4").Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Offset(-1, 0).Select
        Selection.Copy
        Range("A4").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        B = Cells(4, 1).Value + 7
        Sheets("FLUXO DE CAIXA").Select
        Range(Cells(B, 1), Cells(B, 53)).Select
        Selection.ClearContents
        If Cells(8, 1).Value = "" Then
            Range("B7:AA7").Select
            Selection.Copy
            Else
            Range("D1:I1").Select
            Selection.Copy
            Range("Y7").Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
            Range("X7").Select
            Selection.End(xlDown).Offset(0, 6).Select
                Range(Selection, Selection.End(xlUp)).Select
                Range(Selection, Selection.Offset(0, -5)).Select
                Selection.FillDown
            End If
        Range("B7:AD7").Select
    End Sub
    I've mixed the two subs.

  14. #14
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: [help] macro to open several folders

    I don't know how works your code and his goal, then I can not help you, paste here the whole code with comments, I need to know where you are using the opened workbook

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: [help] macro to open several folders

    Hi,patel45,

    maybe you have a look into the Application model of Excel. Any range can only be part of a worksheet and via this of a workbook so your suggested code line
            range("range to copy").copy wbmain.range("pasting range")
    will cause a runtime error as it lacks the worksheet as a link between the workbook and the range given.
            Range("B7:AD" & Range("B" & Rows.Count).End(xlUp).Row).Copy ThisWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    ActiveSheet could be replaced with the name of the sheet which should receive the information.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  16. #16
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Thank's HaHoBe.

    Supose that the name o the sheet is "CSTEST", how the code sould be?
    I've tried
    Range("B7:AD" & Range("B" & Rows.Count).End(xlUp).Row).Copy ThisWorkbook.CSTEST.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    But nothing...

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: [help] macro to open several folders

    Hi, Gila,

    the way you applied CSTEST it would be understood to be the codename of the sheet.

    Range("B7:AD" & Range("B" & Rows.Count).End(xlUp).Row).Copy ThisWorkbook.Sheets("CSTEST").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    And I wonder why in your previous post that sheet name was mentioned as CAIXA.

    Ciao,
    Holger

  18. #18
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    HaHoBe,

    I just change de name, but it makes no difference to me.
    I've tried again, but nothing, it's running error '9' subscript out of range.

    I don't know if count, but I using excel 2013.

  19. #19
    Registered User
    Join Date
    09-20-2013
    Location
    BRAZIL
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: [help] macro to open several folders

    Dim strFile As String
        Dim objFSO, destRow As Long
        Dim mainFolder, mySubFolder
        Set wbmain = ThisWorkbook
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        mFolder = "P:\CONTROLADORIA\FLUXOS EM ANDAMENTO\"
        Set mainFolder = objFSO.GetFolder(mFolder)
        For Each mySubFolder In mainFolder.subfolders
        strFile = Dir(mySubFolder & "\*.xls*")
            Do While strFile <> ""
                Workbooks.Open mySubFolder & "\" & strFile, ReadOnly:=True
         REPETE   
        Range("B7:AD7").Select
        Range(Selection, Selection.End(xlDown)).Copy 
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Selection.End(xlUp).Select
        Selection.End(xlDown).Offset(1, 0).Select
            Application.DisplayAlerts = False
            ActiveWindow.Close
            strFile = Dir
         Loop
       Next
    
    Sub REPETE()
        Sheets("DADOS DA VENDA").Select
        Range("A4").Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Offset(-1, 0).Select
        Selection.Copy
        Range("A4").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        B = Cells(4, 1).Value + 7
        Sheets("FLUXO DE CAIXA").Select
        Range(Cells(B, 1), Cells(B, 53)).Select
        Selection.ClearContents
        If Cells(8, 1).Value = "" Then
            Range("B7:AA7").Select
            Selection.Copy
            Else
            Range("D1:I1").Select
            Selection.Copy
            Range("Y7").Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
            Range("X7").Select
            Selection.End(xlDown).Offset(0, 6).Select
                Range(Selection, Selection.End(xlUp)).Select
                Range(Selection, Selection.Offset(0, -5)).Select
                Selection.FillDown
            End If
        Range("B7:AD7").Select
    End Sub
    I have one spreadsheet named "CAIXA", that is where I consolidate all the data from each spreadsheet opened through the command "Workbooks.Open mySubFolder & "\" & strFile".
    But before I close the workbook, I have to copy the information and paste it into the spreadsheet "CAIXA"!
    Only after that, I may close the recent opened file.

    I you have any doubt, please let me know!
    Thank!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to open folders and pdf file inside the folders
    By kirtesh250187 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2013, 03:17 AM
  2. Replies: 0
    Last Post: 04-15-2013, 06:54 AM
  3. [SOLVED] Macro to open and close all files in a folder and sub folders
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-31-2012, 06:51 AM
  4. open folders
    By slcnrtm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-27-2009, 06:10 PM
  5. [SOLVED] Open files in different folders
    By TEB2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2005, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1