+ Reply to Thread
Results 1 to 10 of 10

Open New Workbook with VBA & Save Name & Location for Use in Other Macros

  1. #1
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    I found some code online that allowed me to open a new workbook and save this workbook as a variable (I think) so that I can refer to it throughout the macro. Problem is, I cannot access this in other macros. Is there another way to write this so I can refer back to it in other macros on this module?

    Please Login or Register  to view this content.

    I'd like for it to save the Core Sheet at the end of all this with the name "Core_999999998_1949_mm_dd.xls" but give the user the option where to save it to. Is that possible?
    Last edited by amyxkatexx; 08-03-2010 at 10:29 AM. Reason: clarification

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    The easy way to pass data from one module to another is to define the variable holding the data as being global.

    At the very start of your module, before any subs add:

    Please Login or Register  to view this content.
    Once your sub has set it you'll be able to read the value from other subs/functions.

    This isn't great programming practice, but it works.

    Better (slightly) would be to make your Sub a Function and pass the value back in a controlled fashion.

  3. #3
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    That works great, thanks!

    So what about this part of the question?

    Quote Originally Posted by amyxkatexx View Post
    I'd like for it to save the Core Sheet at the end of all this with the name "Core_999999998_1949_mm_dd.xls" but give the user the option where to save it to. Is that possible?

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    Ah, the hard bit.

    This little bit of code will tell you where the user has tried to save the file ...

    Please Login or Register  to view this content.
    You'll then need to truncate this at the last backslash and add your own filename.

    Does that help?

  5. #5
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    Can I tell it to save it at some point to where the prompt comes up for them to save and the file name is already there, they just have to pick the location?

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    Yes, just set the Initialfilename...

    Please Login or Register  to view this content.
    This won't save the file, just return the selected path and save name. If you just want to go ahead and save it you can use:

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    If I use that second part though, won't it just save it in a location wherever they last saved something and possibly put it where they can't find it?

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    No, if you use the 2nd part it will open the file save as dialogue and let them select where they want to save the file, it will just auto-populate the save name.

    You can add a default directory path as well, if you want, but be warned - trying to code around user stupidity is an endless, and mainly thankless, task.

  9. #9
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    Okay, so I seem to be unable to figure this one out...

    Without choosing the filepath because these are Macs, so I can't do the whole C:Documents and Settings thing because the file path changes for each user and since Excel for Mac 2004 won't let you use relative filepaths, there's just no way to do this for multiple users.

    So, without doing that, how can I...

    1. Bring up the save as menu
    2. With the name already populated as "Core_999999998_1949_mm_dd.xls" (mm & dd being the actual month and date)
    3. And once they hit save, continue on with my macro (or even if they hit cancel)

    I have a hard time piecing these codes together in the right order, so if you know how to and don't mind doing so, I'd really appreciate the whole code somehow implemented into my original post to where after it pastes the header row...

    Please Login or Register  to view this content.

    ...it then goes through this save process.

    I can't tell you enough how much help you've been so far and I really appreciate your time and patience with me!

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Open New Workbook with VBA & Save Name & Location for Use in Other Macros

    Try this ...

    Please Login or Register  to view this content.
    Let me know if that works for you.

    Edited to add: Hmmm, that doesn't seem to work for me. I'm sure it was earlier. Bear with me.
    Last edited by Andrew-R; 08-03-2010 at 02:30 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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