+ Reply to Thread
Results 1 to 16 of 16

How to open files in excel using VBA

  1. #1
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    How to open files in excel using VBA

    I am working on a project to create a user interface in excel that would have a form with various button, spin buttons, option buttons etc.

    So first thing i want to learn is how to open a file in excel through VBA. This is the code I came up with but it doesn't work.
    I have a folder on my desktop called notes. I am using it as my test folder to learn code I need for this project.
    In the folder I have other folders one is called January the other is February.

    I don't think I will use a msg box for this in the actual project. I will use a spin button for this, but for now this is what I came up with.
    Please Login or Register  to view this content.
    Thank you.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    The returned filename does not contain the full path. So you need to include that when openning the workbook.

    Please Login or Register  to view this content.
    I also suggest you use a variable with a different name to the argument identifier to avoid any confusion
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    So lets say i want to have a spin button that scrolls through each month of the year. And each month is the name of each folder.. how do I do that.. How do i select at folder level not at the file in the folder ..Because after they select folder then i want a form to come up that the user can then use to select the file. Each month will have the same number of files with the same name.
    Example
    folder january
    file 1
    file 2
    folder February
    file 1
    file 2
    folder March
    file 1
    file 2
    each month ..........

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    rather than a spin button why not use a dropdown listbox of month names. The you can use that value instead of the InputBox method you are currently using.

  5. #5
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    Not sure how it would be different. Because each month is related to a folder. After the month(folder) is selected they would have up to five files to choose from within the month(folder).

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    once you know the month you can either populate another control by looping through using the DIR function, as you have.
    Or you could just display a file picking dialog, Application.GetOpenFilename , to get users selection of which file to use.

  7. #7
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    Ok so what would be the code to pull the month that was selected in the first control into the loop of the second control?

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    Please Login or Register  to view this content.
    where your useform has a combobox called cboMonthNames that contains month names.
    the results are used to populate a listbox called lisFilenames

  9. #9
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    Quote Originally Posted by Andy Pope View Post
    Please Login or Register  to view this content.
    where your useform has a combobox called cboMonthNames that contains month names.
    the results are used to populate a listbox called lisFilenames
    Thanks I will give this a try and let you know how it goes.

  10. #10
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    Not sure where i should write this code. Do I put it in the Lisfilenames moduel or the CboMonthNames module/ it looks like it should go in the combo box module but when I tried it gave me an error
    i tried it with this code

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    perhaps it's time you posted an example workbook

  12. #12
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    I tried to do it this way. I have two dropboxes. 1 is for the month =cboMonthNames the other is for the file = cboFileName
    I have a command button right now just titled commandbutton2.
    Here is the code I used but it is not working. I believe I am somehow using the Workbooks.Open incorrectly in my code.

    Please Login or Register  to view this content.
    The error I'm getting is run-time 1004

    strFileName.xlsx could not be found. Check the spelling of the file name, and verify that the file location is correct.

  13. #13
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    Quote Originally Posted by Andy Pope View Post
    perhaps it's time you posted an example workbook
    here is an workbook example
    Attached Files Attached Files

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    the zip contains all .xlsx files.

    Which one has your current code in it?

  15. #15
    Registered User
    Join Date
    06-30-2010
    Location
    Newport New VA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Re: How to open files in excel using VBA

    sorry didnt know you wanted that too.. attaching it here..
    Attached Files Attached Files

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: How to open files in excel using VBA

    It's not very clear from your file what is suppose to be happening.

    But this should open the correct file based on the selection of items from combo boxes on frmUsrDataSheet

    Please Login or Register  to view this content.

+ 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. [SOLVED] Save/Closed only macro related files and leave unrelated files open in excel?
    By rdowney79 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2013, 04:17 PM
  2. one folder & so many excel files are save, i want name change without open excel files
    By sonu_kumar444 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 03:45 PM
  3. [SOLVED] Open all files in a SharePoint folder (Problem: Files open out of order)
    By holmstarrunner in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2012, 10:13 AM
  4. Excel 2007 : Excel does not open files - only macro files
    By freeskier19 in forum Excel General
    Replies: 0
    Last Post: 02-02-2012, 07:42 PM
  5. Replies: 1
    Last Post: 01-31-2012, 09:07 PM

Tags for this Thread

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