+ Reply to Thread
Results 1 to 23 of 23

[help] macro to open several folders

  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

    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.

  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
    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.

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

  9. #9
    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]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/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]

  10. #10
    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

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

    Smile Re: [help] macro to open several folders

    Please Login or Register  to view this content.
    I've mixed the two subs.

  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

    use this line for opening files
    Please Login or Register  to view this content.
    then use wb1 for referencing ranges and closing opened workbook

  13. #13
    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!

  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
    Please Login or Register  to view this content.
    will cause a runtime error as it lacks the worksheet as a link between the workbook and the range given.
    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.
    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!!

  17. #17
    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
    Please Login or Register  to view this content.
    But nothing...

  18. #18
    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.

    Please Login or Register  to view this content.
    And I wonder why in your previous post that sheet name was mentioned as CAIXA.

    Ciao,
    Holger

  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

    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.

  20. #20
    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,

    Error 9 means that no sheet of the given name is present in the workbook that holds the code, and that is something I canīt do anything about as I donīt know neither how your workbook looks nor the name of the sheets in that workbook.

    Ciao,
    Holger

  21. #21
    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,

    I will try to get a working copy of your codes (there are a couple of flwas in there which you may explain in the meantime).

    Within REPETE you use a If...Then...Else-Statement in which you copy in both statements but only paste in the Else-Statement. Is that by default as I would have expected the paste to be made after the end of the If-Statement. Besides I would not relate on Selection as much as you do in the code (I assume this macro recorder code) which could be simplified from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    from looking at the code on itīs own.

    Ciao,
    Holger

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

    Re: [help] macro to open several folders

    Hi HaHoBe thanks for the tips!

    I'm sorry HaHobe, I made a mistake between sheet and workbook!
    I will try to resume my situation:

    Please Login or Register  to view this content.
    All workbooks "FLUXO" has the same structure, so the sub REPETE it is just to copy the data that I need. <<<<<<<< That I already have too!
    Please Login or Register  to view this content.
    The problem is: Now that I have the data from the recently "FLUXO" opened, I have to past it into a mother workbook "CSTEST", the name of the sheet (inside "CSTEST") that will receive the data may be anyone...to simplify let's just call it "CAIXA". After pasted in "CAIXA" I must close the "FLUXO" before doing LOOP, and go to the next "FLUXO_1".

    Please Login or Register  to view this content.
    "CSTEST" contain all the MACROS and SUBS, and it must be remain open until finish the "FLUXO_n".

    Thank you!!

  23. #23
    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,

    to me there is no use in opening workbooks as ReadOnly but work on them as you could close the workbooks without saving at the end - so what should be the benefit of doing so?.

    This code is untested (I donīt know how your data looks like), and I somehow am irritated by
    the name of the sheet (inside "CSTEST") that will receive the data may be anyone
    which isnīt what I am looking for if Iīm trying to write code.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    In Repete I may be wrong about the code line
    Please Login or Register  to view this content.
    which may be moved into the Else-Statement instead of having it after the If-Statement (right now that line would copy that area at any time).

    Ciao,
    Holger

+ 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