+ Reply to Thread
Results 1 to 5 of 5

How to get the save file box to open when running this macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-19-2012
    Location
    houston
    MS-Off Ver
    excel 2013
    Posts
    103

    How to get the save file box to open when running this macro

    Hi...

    I have a macro that I run to create a new spreadsheet with some data from my workbook. It works great, however it doesn't give me the option to pick the location where I want to save the spreadsheet to. Here is the current code...

    
    
        'Get path for desktop of user PC
        Path = Environ("USERPROFILE")
        ActiveSheet.Range("F4:M" & Range("F" & Rows.Count).End(xlUp).Row).Copy            '<-- Adjust range here
       
        'Create new workbook and past copied data in new workbook & save to desktop
        Workbooks.add (xlWBATWorksheet)
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWorkbook.ActiveSheet.Range("A1").Select
        ActiveWorkbook.ActiveSheet.Name = "Sheet1"
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & "New_" & Format(CStr(Now()), "yyyymmdd\_hhmm") & ".xlsx"
        ActiveWorkbook.Close SaveChanges:=True
    What I would like to change is when the macro runs it first opens the save file box so that I can select what location on my computer that I want to save the Excel file to instead of defaulting to the desktop. I think I need
    to add something like below that is in another macro that does the same thing except for PDF's and it opens the save file box.

    vFile = Application.GetSaveAsFilename _
        (InitialFileName:=sFile, _
            FileFilter:="PDF Files (*.pdf), *.pdf", _
            Title:="Select Folder and File Name to save")

    Just not sure what to change or how to add it to the first code. Any help pointing me in the right direction would be appreciated.

    Thanks!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,870

    Re: How to get the save file box to open when running this macro

    I have done this before but haven't tested this exact code. If it doesn't work, be specific about what doesn't work.
        Dim FilePath As String
    
        'Get path for desktop of user PC
        Path = Environ("USERPROFILE")
        ActiveSheet.Range("F4:M" & Range("F" & Rows.Count).End(xlUp).Row).Copy            '<-- Adjust range here
       
        'Create new workbook and past copied data in new workbook & save to desktop
        Workbooks.add (xlWBATWorksheet)
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWorkbook.ActiveSheet.Range("A1").Select
        ActiveWorkbook.ActiveSheet.Name = "Sheet1"
        FilePath = Application.GetSaveAsFilename _
        (InitialFileName:=sFile, _
            FileFilter:="Excel Files (*.xlsx), *.xlsx", _
            Title:="Select Folder and File Name to save")
        If FilePath = "" Then
            MsgBox "No file selected, file not saved."
        Else
           ActiveWorkbook.SaveAs Filename:=FilePath
           ActiveWorkbook.Close SaveChanges:=True
        End If
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    05-19-2012
    Location
    houston
    MS-Off Ver
    excel 2013
    Posts
    103

    Re: How to get the save file box to open when running this macro

    Hey 6StringJazzer... Thank you!

    It's almost perfect. The only thing that seems odd... is a blank spreadsheet opens up in front of my workbook and then the save dialogue box opens in front of the blank spreadsheet. Once I click save the blank spreadsheet closes along with the dialogue box. Not sure if that is what is supposed to happen or if there is a way to only have the dialogue box open and not have the blank spreadsheet open also?

    Also notice that if you click "Cancel" on the dialogue box it still saves the spreadsheet with the name FALSE.

    Thanks again!
    Last edited by barman; 11-07-2021 at 08:16 PM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,870

    Re: How to get the save file box to open when running this macro

    I'm not sure why you would see a blank spreadsheet. I have not seen that happen. If you attach your actual file I can try it.

    Make this change to deal with the Cancel scenario:

        Dim FilePath As Variant
    
        'Get path for desktop of user PC
        Path = Environ("USERPROFILE")
        ActiveSheet.Range("F4:M" & Range("F" & Rows.Count).End(xlUp).Row).Copy            '<-- Adjust range here
       
        'Create new workbook and past copied data in new workbook & save to desktop
        Workbooks.add (xlWBATWorksheet)
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWorkbook.ActiveSheet.Range("A1").Select
        ActiveWorkbook.ActiveSheet.Name = "Sheet1"
        FilePath = Application.GetSaveAsFilename _
        (InitialFileName:=sFile, _
            FileFilter:="Excel Files (*.xlsx), *.xlsx", _
            Title:="Select Folder and File Name to save")
        If FilePath <> False Then
            MsgBox "No file selected, file not saved."
        Else
           ActiveWorkbook.SaveAs Filename:=FilePath
           ActiveWorkbook.Close SaveChanges:=True
        End If

  5. #5
    Forum Contributor
    Join Date
    05-19-2012
    Location
    houston
    MS-Off Ver
    excel 2013
    Posts
    103

    Re: How to get the save file box to open when running this macro

    I played around with it and after moving somethings around it now works perfect. Adding the screen updating seems to be what stopped the sheet from opening. Thank you again for the help! Here is the final code I ended up with...

         Dim Path As Variant
    
         Application.ScreenUpdating = False
    
        'Get path for desktop of user PC
        FilePath = Application.GetSaveAsFilename _
        (InitialFileName:=ThisWorkbook.Path & "\" & "NEW_" & Format(CStr(Now()), "yyyymmdd\_hhmm") & ".xlsx", _
            FileFilter:="Excel Files (*.xlsx), *.xlsx", _
            Title:="Select Folder and File Name to save")
        If FilePath <> "False" Then
        Path = Environ("USERPROFILE")
        ActiveSheet.Range("F4:M" & Range("F" & Rows.Count).End(xlUp).Row).Copy            '<-- Adjust range here
       
        'Create new workbook and past copied data in new workbook & save to desktop
        Workbooks.add (xlWBATWorksheet)
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveWorkbook.ActiveSheet.Range("A1").Select
        ActiveWorkbook.ActiveSheet.Name = "Sheet1"
        ActiveWorkbook.SaveAs Filename:=FilePath
        ActiveWorkbook.Close SaveChanges:=True
    
        End If
    
        Application.ScreenUpdating = True
    Last edited by barman; 11-08-2021 at 01:46 PM.

+ 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] Excel file won't save after running macro
    By Noob-Noob in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-27-2019, 03:30 PM
  2. VBA macro not running with excel file open
    By gerald.lebret in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2016, 09:33 PM
  3. [SOLVED] VBA Open File/Run Macro/Close &Save/Open Next File
    By JPSIMMON in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2015, 12:16 PM
  4. Macro needed to open "File Save As" and change save file type
    By blainerhett in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2014, 02:40 PM
  5. Running Macro to open other worksheets then save as .csv
    By Martin29 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-15-2013, 10:13 AM
  6. Running a macro on file open
    By emea training 2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2007, 06:23 AM
  7. How to stop getting the file save box when running a macro
    By Pank Mehta in forum Excel General
    Replies: 1
    Last Post: 03-29-2005, 12: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