+ Reply to Thread
Results 1 to 12 of 12

sheet not saving in correct filepath, instead its saving it to My Documents

Hybrid View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    sheet not saving in correct filepath, instead its saving it to My Documents

    I have the following code, working except that its not saving the new spreadsheet in the correct folder. (instead it is saving it to My Documents folder). I need it to save in the parent folder of the initial spreadsheet.
    I added a line (now commented out) that does save the file in the correct path, but then the macro does a dump and fails further down the line.
    Sub CreatePO()
        'copy sheet to new workbook
        Dim wb As Workbook
        
        abc = ActiveSheet.Name
        Sheets("formulas").Range("AB1") = Sheets(abc).Range("I39")
        
        Set wb = Workbooks.Add
        
        ThisWorkbook.ActiveSheet.Copy Before:=wb.Sheets(1)
    
        'save workbook as xlsm
        wb.SaveAs ActiveSheet.Name, FileFormat:=52
        'wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    
        Dim openworkbook
        Dim x1
        
        Dim FNametxt As String, FNamefrm As String, FileSelected As String, FileName As String
            With ThisWorkbook
            Let FNametxt = .Path & "\PO\code.txt"
            .VBProject.VBComponents("Module1").Export FNametxt
            Let FNamefrm = .Path & "\PO\form.frm"
            .VBProject.VBComponents("UserForm1").Export FNamefrm
            Let FNamecal = .Path & "\PO\calendar.frm"
            .VBProject.VBComponents("frmCalendar").Export FNamecal
            End With
    
        Workbooks.Open abc
    
        Sheets(abc).Activate
        ActiveWorkbook.VBProject.VBComponents.Import FNametxt
        ActiveWorkbook.VBProject.VBComponents.Import FNamefrm
        ActiveWorkbook.VBProject.VBComponents.Import FNamecal
        ActiveWorkbook.Save
        
        Set openworkbook = Workbooks.Open(abc)
         
        Application.ScreenUpdating = False
        
         With ThisWorkbook
            .Sheets("Formulas").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Master").Copy Before:=openworkbook.Sheets(1)
            .Sheets("BigMaster").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Estimating1").Copy Before:=openworkbook.Sheets(1)
            .Activate
        End With
        
        'close purchase order-template, DONT SAVE
        Workbooks("Purchase Order-template.xlsm").Activate
            'Application.DisplayAlerts = False
            'ActiveWorkbook.Close
            'Application.DisplayAlerts = True
            'another way to close the file without seeing any prompts
        Workbooks("Purchase Order-template.xlsm").Close SaveChanges:=False
      End Sub
    when i uncomment out this line, the macro fails

        'wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    can anyone help me with this?

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    I don't see anything wrong with that. I seldom rely on relative file references. I use full drive:\path\filename.ext paths.

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    when i put the full path in, the macro fails here:

         With ThisWorkbook
            .Sheets("Formulas").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Master").Copy Before:=openworkbook.Sheets(1)
            .Sheets("BigMaster").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Estimating1").Copy Before:=openworkbook.Sheets(1)
            .Activate
        End With
    thoughts?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    If you don't specify the path to save file it will be saved in what Excel/VBA sees as the current directory, which may or may not be what you want/think it is.

    How does the code fail when you use this?
    wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    Also, why are you saving the newly added workbook here,
        wb.SaveAs ActiveSheet.Name, FileFormat:=52
    then, even though you haven't closed it, re-opening it here,
     Workbooks.Open abc
    and then again re-opening it.
     Set openworkbook = Workbooks.Open(abc)
    Actually, could you explain what the code is actually meant to do?

    Is it meant to save a specific worksheet from the workbook the code is in to a new workbook and add code from the original workbook to the new workbook?
    If posting code please use code tags, see here.

  5. #5
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    yes this statement is correct: Is it meant to save a specific worksheet from the workbook the code is in to a new workbook and add code from the original workbook to the new workbook?

    i have a workbook that contains multiple PO tabs. i need to save them as i use them. so when i put this in my code

    wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    and that code works, it saves it into the parent directory of the source file.

    but when i do that it fails here:

         With ThisWorkbook
            .Sheets("Formulas").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Master").Copy Before:=openworkbook.Sheets(1)
            .Sheets("BigMaster").Copy Before:=openworkbook.Sheets(1)
            .Sheets("Estimating1").Copy Before:=openworkbook.Sheets(1)
            .Activate
        End With

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    What exactly do you want/not want in the new 'PO' workbook(s)?

  7. #7
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    so i made a change to the code, it's failing on Workbooks("Purchase order-Template").Sheets("Formulas").Copy Before:=openworkbook.Sheets(1), gives me a run time error '91', object variable or with block variable not set

    Sub CreatePO()
        'copy sheet to new workbook
        Dim wb As Workbook
        
        abc = ActiveSheet.Name
        Sheets("formulas").Range("AB1") = Sheets(abc).Range("I39")
        
        Set wb = Workbooks.Add
        
        ThisWorkbook.ActiveSheet.Copy Before:=wb.Sheets(1)
        
        'save workbook as xlsm
        'wb.SaveAs ActiveSheet.Name, FileFormat:=52
        wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    
        Dim openworkbook
        Dim x1
        
        Dim FNametxt As String, FNamefrm As String, FileSelected As String, FileName As String
            With ThisWorkbook
            Let FNametxt = .Path & "\PO\code.txt"
            .VBProject.VBComponents("Module1").Export FNametxt
            Let FNamefrm = .Path & "\PO\form.frm"
            .VBProject.VBComponents("UserForm1").Export FNamefrm
            Let FNamecal = .Path & "\PO\calendar.frm"
            .VBProject.VBComponents("frmCalendar").Export FNamecal
            End With
    
        Workbooks.Open abc
    
        Sheets(abc).Activate
        ActiveWorkbook.VBProject.VBComponents.Import FNametxt
        ActiveWorkbook.VBProject.VBComponents.Import FNamefrm
        ActiveWorkbook.VBProject.VBComponents.Import FNamecal
        ActiveWorkbook.Save
        
        Set openworkbook = Workbooks.Open(abc)
         
        Application.ScreenUpdating = False
        
         'With Workbooks("Purchase order-Template")
            Workbooks("Purchase order-Template").Activate
            Workbooks("Purchase order-Template").Sheets("Formulas").Copy Before:=openworkbook.Sheets(1)
            Workbooks("Purchase order-Template").Sheets("Master").Copy Before:=openworkbook.Sheets(1)
            Workbooks("Purchase order-Template").Sheets("BigMaster").Copy Before:=openworkbook.Sheets(1)
            Workbooks("Purchase order-Template").Sheets("Estimating1").Copy Before:=openworkbook.Sheets(1)
            Workbooks("Purchase order-Template").Activate
        'End With
        
        'close purchase order-template, DONT SAVE
        Workbooks("Purchase Order-template.xlsm").Activate
            'Application.DisplayAlerts = False
            'ActiveWorkbook.Close
            'Application.DisplayAlerts = True
            'another way to close the file without seeing any prompts
        Workbooks("Purchase Order-template.xlsm").Close SaveChanges:=False
    
    End Sub
    thoughts?

  8. #8
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    so the new workbook should contain just one PO, add code to it, add supporting sheets for the macro to run.

    the issue is where to put the "With ThisWorkbook" statements so that they work.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,711

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    Probably missing something here, but the variable abc, as far as I can see contains a sheet name. Where do you add a file extension to the file name of the workbook?
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  10. #10
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    Quote Originally Posted by TMS View Post
    Probably missing something here, but the variable abc, as far as I can see contains a sheet name. Where do you add a file extension to the file name of the workbook?
    the format is found on this line:

    wb.SaveAs ThisWorkbook.Path & "\" & abc, FileFormat:=52
    which saves it as a .xlsm format

  11. #11
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    so i fixed it. made the following changes and now it is working as expected.

         With Workbooks("Purchase order-Template.xlsm")
            .Activate
            .Sheets("Formulas").Copy Before:=Workbooks(abc).Sheets(1)
            .Sheets("Master").Copy Before:=Workbooks(abc).Sheets(1)
            .Sheets("BigMaster").Copy Before:=Workbooks(abc).Sheets(1)
            .Sheets("Estimating1").Copy Before:=Workbooks(abc).Sheets(1)
            'Workbooks("Purchase order-Template.xlsm").Activate
        End With

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,711

    Re: sheet not saving in correct filepath, instead its saving it to My Documents

    Might save it with that format, but does it add the extension for you? I've always created the full file path, file name and file extension. Something I must try

    Thanks for the rep.

+ 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 not saving into FilePath
    By Keith Edgar in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-27-2014, 08:14 AM
  2. [SOLVED] Macro to save to Network Drive is saving to My Documents
    By MooseAUH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2014, 07:26 AM
  3. [SOLVED] Complex Parsing and Saving as Multiple Text Documents
    By johnph in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-29-2012, 04:13 PM
  4. Word not saving documents correctly
    By J.wills in forum Word Programming / VBA / Macros
    Replies: 5
    Last Post: 03-27-2009, 06:08 AM
  5. [SOLVED] saving documents with macros
    By shrek in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-10-2005, 06:20 PM
  6. [SOLVED] Saving New Documents on a network Drive.
    By MaxZ in forum Excel General
    Replies: 1
    Last Post: 11-09-2005, 06:35 PM
  7. Saving to users My Documents
    By Rob Edwards in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2005, 06:07 AM
  8. Open documents interfering with saving
    By mgasparel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-14-2005, 05:26 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