+ Reply to Thread
Results 1 to 18 of 18

vba to find and open specific folder

  1. #1
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    vba to find and open specific folder

    Dear , I have excel worksheet contains command button this command button named city and I have folder named city as command button name contains many files. I need a vba to search in directories C and D… etc to find folder named city ( like command button name) , if the folder does not exist I want to get message that folder does not exist , and if there are more than one folder in the same name city I want to list all and I will choose one of them to open it , and if there is just one folder in the name city I want vba to open it directly.

    Regards

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    Set a reference to MS Scripting Runtime, and use Form commandbuttons (Not ActiveX commandbuttons), assigning the macro "FindCity" to each button: the caption will determine which city folder is found: (You may need to limit which folders are searched on C, because system folders can be a problem for auto-processing) - I'm not sure what you meant by "open the folder" so that part may need to wait.

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 01-11-2016 at 01:34 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie ,
    All I want :
    I have folder named city contains many excel files, and I have excel sheet contains a command button , I named the caption of this command button as the same of the folder city, I want macro to search and open the folder when I press the command button (search in both directories C & D ), then I will choose manually the file I want . also if the folder not exist I want a message that folder not exist , and if there are more than one folder in same name I want a list of all folder found then I will choose which one I want to open.
    Thanks for your help

  4. #4
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Berine ,
    I'm new in vba , so I creat a command button in sheet1 and and edited its text to FindCity ,then copied your code and paste it in this command button , but when I press the command button I got a message ' compile error: user-define type not defined' . what should I do to let the code work? .
    and don't know , what do you mean 'set a reference to MS scripting Runtime'
    Note : I use excell 2003 .
    Hope your help
    Regards

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    First, for the reference - read about it here:

    http://www.cpearson.com/excel/References.htm

    Then look for the file named "Microsoft Scripting Runtime" within your library. See the picture...

    References.JPG

    As for the button, you must use a Forms button, not an ActiveX button. Right-click your commandbar and select the "Form controls" (I forget the specific wording, but that is toolbar to choose the commandbutton from. My code goes into a standard codemodule, and you need to assign the macro "FindCity" to that commandbutton. I have attached a working file, here: Find Folders only.xls The button will find folders with "Excel" in their name until you edit the button's caption.....
    Last edited by Bernie Deitrick; 01-12-2016 at 09:52 AM.

  6. #6
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie, thanks for Helping me .
    i saved your excel file on desktop of my computer and i changed the "Excel" caption of command button to "City" , then i enter to Reference from tool in VBE, i found " Microsoft Scripting Runtime " is Checked .
    when i press the command button , " Run-Time Error:70 permission Denied " , when i press on Debug : the line " If sfldr.SubFolders.Count > 0 Then " in the below code get Yellow , so where is my mistake ? please help me

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 11-19-2019 at 02:26 PM.

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    You are running into operating system limitations - from my first post:

    (You may need to limit which folders are searched on C, because system folders can be a problem for auto-processing)
    So instead of C:\ as the root folder on C, start where your folders actually are or use two or three if they can be in different parent folders:

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie, I am shying from you because I weak I vba , but the code above doesn't work yet.
    My computer is just 2 pratitions " C " and " D " . and my folder exist on part "D" but i want the macro search in all partitons 'C' and all 'D' to the folder name as caption of command button and open it .
    Regards and thanks

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    There are folders on C that you cannot process, so you must limit the search program. You cannot put your files into folders into those places anyway, so just limit the search to the places that you know have your files.
    Please Login or Register  to view this content.
    If you cannot control where the folder exists then you really shouldn't be using C at all.

  10. #10
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    I delete line of "set RootFolder=FileSysObject.GetFolder("C:\users\username\document") at all , and let just line of RootFolder… etc of "D" , also I got the same error that I mention above Runtime error 70 , permidsion denied .
    Why ? Although I saved your excel file as it on my computer and I rename the caption of command button .

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    You may have the same permissions issue with D: - it all depends on your system. You must know where the files could be - so limit the folders that are searched. Otherwise, it just won't work.

  12. #12
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie , if you please to help me ,
    May you send me a new working excel file contain command button to open my specific folder named as caption of command button and my folder exactly exist on desktop .
    Note : i want the macro ignor in the path the owner of computer . because i want the macro still wotking if I copy the file to another computer ( to the new computer owner )
    And if there is any change in macro to be suitable for me please let me know where and how .
    Hope your help

    Regards

  13. #13
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    This will look on the desktop of the current machine:


    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Berinie , sorry to inconvenience you ,
    When I run the macro I got this message 'run-time error 438: object doesn't support this property or method ' and the macro line "FindFolderName = ActiveSheet. Buttons(Application. Caller). Caption" get yellow.
    Note: I worked to another computer to see if the problem from my own computer. But also still the macro not working . am I have to add or cancel check of some reference rather than microsoft scripting run time . I need this macro and I'm upset.

  15. #15
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    Edit the text on the buttons to match some of the folders on your desktop, and try this file:

    Find Folders only.xlsm

  16. #16
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie , i'm very thankfull , the code works perfect , and i'm sorry to inconveniance you .

    Besr Regard

  17. #17
    Forum Contributor
    Join Date
    06-15-2012
    Location
    beirut
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: vba to find and open specific folder

    Dear Bernie ,
    your code is working very well .
    but in the folder i have Excel files and some images , when i press the button and open the folder i can open normally the excel files but the images doesn't open by double click it open file excel instead contains some letters and symbol . how can i solve this problem and open the images by double click on it ?
    also can you make a macro like the macro above to open an excel file have name the same caption of command button ? also the excel files exist in folders exist on desktop .

    Best Regards

  18. #18
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,346

    Re: vba to find and open specific folder

    The code is meant to open Excel files, so image files are not handled. That is a completely different issue, and you should start a new message string to get help with that problem.

+ 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. Code to open a specific folder
    By petitpiti in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-21-2014, 11:02 AM
  2. [SOLVED] Need GetSaveAsFilename to open in specific folder
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2014, 06:14 AM
  3. Open Excel to a specific folder
    By Excel_vba in forum Excel General
    Replies: 2
    Last Post: 03-10-2014, 09:15 PM
  4. Open all xls in folder with specific date
    By 13lack13lade in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-03-2014, 02:22 AM
  5. [SOLVED] Macro to open specific folder
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-16-2013, 04:22 PM
  6. [SOLVED] VBA-Open At Specific Folder with BrowseForFolder
    By michaelisk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-08-2013, 05:51 PM
  7. [SOLVED] Open specific folder, select file to open and copy then paste
    By Kranky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-22-2012, 12:14 AM

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