SOLVED

SOLVED - "Method 'SaveAs' of object '_Workbook' failed" (1004) when saving into same file location

%3CLINGO-SUB%20id%3D%22lingo-sub-3249728%22%20slang%3D%22en-US%22%3ESOLVED%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20file%20location%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3249728%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BI%20have%20code%20designed%20to%20create%20an%20archive%20of%20my%20main%20sheet%20(as%20a%20.xlsx)%20by%20copying%20the%20sheet%20%26gt%3B%20saving%20the%20copied%20sheet%20in%20a%20new%20workbook%20%26gt%3B%20doing%20things%20to%20the%20sheet%20within%20the%20new%20workbook%20%26gt%3B%20saving%20and%20closing%20new%20workbook%20then%20continuing%20the%20rest%20of%20my%20code.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEverything%20works%20as%20coded%20except%20when%20the%20user%20%3CSTRONG%3Eselects%20(or%20keeps%20selected)%20the%20same%20file%20location%3C%2FSTRONG%3E%2C%20in%20the%20SaveAs%20dialog%2C%20that%20the%20original%20file%20(with%20the%20running%20VBA)%20is%20in.%20It%20%3CSTRONG%3Ereturns%20a%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%3C%2FSTRONG%3E%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BI%20created%20an%20%22If%22%20check%20to%20see%20if%20the%20selected%20file%20location%20from%20the%20SaveAs%20dialog%20is%20the%20same%20as%20the%20file%20location%20of%20the%20original%20and%20was%20able%20to%20create%20an%20error%20handler%20(avoid%20the%20error)%2C%20but%20not%20an%20error%20solution.%3CSTRONG%3E%20I%20want%20to%20default%20to%20the%20same%20file%20location%20as%20the%20original%2C%20and%20regardless%20I%20want%20the%20user%20to%20be%20able%20to%20save%20into%20the%20same%20file%20location%2C%20especially%20since%20that%20is%20a%20very%20typical%20thing%20to%20do.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BLine%20(59)%20with%20error%201004%3A%3C%2FP%3E%3CPRE%3EActiveWorkbook.SaveAs%20fileName%3A%3DPathAndFile_Name%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%3C%2FPRE%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BShiftYear%20(what%20code%20is%20in)%20and%20PleaseWait%20are%20userforms%2C%20and%26nbsp%3B%22Troop%20to%20Task%20-%20Tracker%22%20is%20the%20sheet%20I'm%20copying.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECode%20with%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E'%3CPREVIOUS%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%3E%0A%0A'''Declare%20variables%3A%0A'General%3A%0ADim%20NewGenYear%20As%20Integer%2C%20LastGenYear%20As%20Integer%2C%20year_create_counter%20As%20Integer%0ANewGenYear%20%3D%200%3A%20LastGenYear%20%3D%200%3A%20year_create_counter%20%3D%200%0A'Personnel%3A%0ADim%20cell_person%20As%20Range%2C%20cell_num%20As%20Range%0ADim%20cell_num_default%20As%20Range%0A'Archive%3A%0ADim%20Sheet_Archive%20As%20Worksheet%2C%20ShVal%20As%20Integer%0ADim%20ObFD%20As%20FileDialog%0ADim%20File_Name%20As%20String%0ADim%20PathAndFile_Name%20As%20String%0ADim%20Shape_Clr%20As%20Shape%0ADim%20cell_color_convert%20As%20Range%0A%0A'%3CA%20whole%3D%22%22%20bunch%3D%22%22%20of%3D%22%22%20other%3D%22%22%20checks%3D%22%22%20and%3D%22%22%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%20target%3D%22_blank%22%3E%0A%0A'Set%20then%20launch%20SaveAs%20dialog%3A%0AIf%20ShiftYear.CheckBox5.Value%20%3D%20True%20Then%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20Checked%3A%0A%20%20%20%20For%20Each%20Sheet_Archive%20In%20ThisWorkbook.Sheets%0A%20%20%20%20Select%20Case%20Sheet_Archive.CodeName%0A%20%20%20%20Case%20Is%20%3D%20%22Sheet4%22%2C%20%22Sheet5%22%2C%20%22Sheet6%22%2C%20%22Sheet7%22%0A%20%20%20%20ShVal%20%3D%20Sheet_Archive.Name%0A%20%20%20%20If%20Sheet_Archive.Range(%22A2%22).Value%20%26lt%3B%26gt%3B%20%22N%2FA%22%20And%20ShVal%20%26lt%3B%26gt%3B%20ShiftYear.Shift_3.Value%20Then%0A%20%20%20%20File_Name%20%3D%20%22Archive%20%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22_%22%20%26amp%3B%20ThisWorkbook.Name%20'Set%20default%20(suggested)%20File%20Name%0A%20%20%20%20Set%20ObFD%20%3D%20Application.FileDialog(msoFileDialogSaveAs)%0A%20%20%20%20With%20ObFD%0A%20%20%20%20%20%20%20%20.Title%20%3D%20%22Archive%20Year(s)%20-%20Personnel%20Tracker%22%0A%20%20%20%20%20%20%20%20.ButtonName%20%3D%20%22A%26amp%3Brchive%22%0A%20%20%20%20%20%20%20%20.InitialFileName%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20File_Name%20'Default%20file%20location%20and%20File%20Name%0A%20%20%20%20%20%20%20%20.FilterIndex%20%3D%201%20'File%20Type%20(.xlsx)%0A%20%20%20%20%20%20%20%20.AllowMultiSelect%20%3D%20False%0A%20%20%20%20%20%20%20%20.InitialView%20%3D%20msoFileDialogViewDetails%0A%20%20%20%20%20%20%20%20.Show%0A%20%20%20%20%20%20%20%20If%20.SelectedItems.count%20%3D%200%20Then%0A%20%20%20%20%20%20%20%20MsgBox%20%22Generation%20and%20archiving%20canceled.%20No%20year(s)%20were%20created%2C%20shifted%2C%20or%20overwritten.%20To%20continue%20generating%20without%20archiving%2C%20uncheck%20the%20%22%22Archive%20%26lt%3B%3D5%20year(s)%20calendar%2Fpersonnel%20data%20externally%20before%20overwriting%22%22%20box%20then%20click%20%22%22Generate%22%22%20again.%22%20_%0A%20%20%20%20%20%20%20%20%2C%20vbExclamation%2C%20%22Year%20Shift%20%26amp%3B%20Creation%20-%20Personnel%20Tracker%22%0A%20%20%20%20%20%20%20%20'%3CMY%20code%3D%22%22%20that%3D%22%22%20turns%3D%22%22%20off%3D%22%22%20macro%3D%22%22%20enhancement%3D%22%22%3E%0A%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20PathAndFile_Name%20%3D%20.SelectedItems(1)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20With%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20%0A'Load%20year%20to%20be%20archived%3A%0A%20%20%20%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22I7%22).Value%20%3D%20Sheet_Archive.Name%0A%20%20%20%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22I13%22).Value%20%3D%20%22No%22%0A%20%20%20%20Call%20Load_Year.Load_Year%0A%0A'Copy%20Troop%20to%20Task%20-%20Tracker%20sheet%20into%20new%20workbook%20and%20format%3A%0A%20%20%20%20PleaseWait.Label2.Caption%20%3D%20%22Creating%20%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22%20archive%20file%20...%22%0A%20%20%20%20DoEvents%0A%20%20%20%20File_Name%20%3D%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22%5C%22))%20'Update%20File%20Name%20to%20user's%20input%0A%20%20%20%20ThisWorkbook.Sheets(%22Troop%20to%20Task%20-%20Tracker%22).Copy%0A%0A%20%20%20%20ActiveWorkbook.SaveAs%20fileName%3A%3DPathAndFile_Name%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%20'New%20workbook%20save%20and%20activate%0A%0A%20%20%20%20'%3CALL%20my%3D%22%22%20code%3D%22%22%20that%3D%22%22%20changes%3D%22%22%20the%3D%22%22%20new%3D%22%22%20workbook%3D%22%22%3E%0A%0A%20%20%20%20Excel.Workbooks(File_Name).Activate%0A%20%20%20%20Excel.Workbooks(File_Name).Close%20savechanges%3A%3DTrue%20'New%20workbook%20save%20and%20close%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20End%20If%0A%20%20%20%20End%20Select%0A%20%20%20%20If%20(Sheet_Archive.CodeName%20%3D%20%22Sheet4%22%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet5%22%20_%0A%20%20%20%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet6%22%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet7%22)%20_%0A%20%20%20%20And%20ShVal%20%26lt%3B%26gt%3B%20ShiftYear.Shift_3.Value%20Then%0A%20%20%20%20PleaseWait.Label2.Caption%20%3D%20%22%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22%20archive%20file%20complete%22%0A%20%20%20%20DoEvents%0A%20%20%20%20Else%3A%20PleaseWait.Label2.Caption%20%3D%20%22Initailizing%20archive%20...%22%0A%20%20%20%20DoEvents%3A%20End%20If%0A%20%20%20%20Next%20Sheet_Archive%0AElseIf%20ShiftYear.CheckBox5.Value%20%3D%20False%20Then%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20Unchecked%3A%0A%20%20%20%20'Do%20Nothing%0AEnd%20If%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20END%0A%0A'%3CCONTINUING%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%3E%3C%2FCONTINUING%3E%3C%2FALL%3E%3C%2FMY%3E%3C%2FA%3E%3C%2FPREVIOUS%3E%3C%2FCODE%3E%3CA%20whole%3D%22%22%20bunch%3D%22%22%20of%3D%22%22%20other%3D%22%22%20checks%3D%22%22%20and%3D%22%22%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%20target%3D%22_blank%22%3E%3C%2FA%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECode%20with%20error%20handler%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E'%3CPREVIOUS%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%3E%0A%0A'''Declare%20variables%3A%0A'General%3A%0ADim%20NewGenYear%20As%20Integer%2C%20LastGenYear%20As%20Integer%2C%20year_create_counter%20As%20Integer%0ANewGenYear%20%3D%200%3A%20LastGenYear%20%3D%200%3A%20year_create_counter%20%3D%200%0A'Personnel%3A%0ADim%20cell_person%20As%20Range%2C%20cell_num%20As%20Range%0ADim%20cell_num_default%20As%20Range%0A'Archive%3A%0ADim%20Sheet_Archive%20As%20Worksheet%2C%20ShVal%20As%20Integer%0ADim%20ObFD%20As%20FileDialog%0ADim%20File_Name%20As%20String%0ADim%20PathAndFile_Name%20As%20String%0ADim%20Shape_Clr%20As%20Shape%0ADim%20cell_color_convert%20As%20Range%0A%0A'%3CA%20whole%3D%22%22%20bunch%3D%22%22%20of%3D%22%22%20other%3D%22%22%20checks%3D%22%22%20and%3D%22%22%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%20target%3D%22_blank%22%3E%0A%0A'Set%20then%20launch%20SaveAs%20dialog%3A%0AIf%20ShiftYear.CheckBox5.Value%20%3D%20True%20Then%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20Checked%3A%0A%20%20%20%20For%20Each%20Sheet_Archive%20In%20ThisWorkbook.Sheets%0A%20%20%20%20Select%20Case%20Sheet_Archive.CodeName%0A%20%20%20%20Case%20Is%20%3D%20%22Sheet4%22%2C%20%22Sheet5%22%2C%20%22Sheet6%22%2C%20%22Sheet7%22%0AArchive_Error%3A%0A%20%20%20%20ShVal%20%3D%20Sheet_Archive.Name%0A%20%20%20%20If%20Sheet_Archive.Range(%22A2%22).Value%20%26lt%3B%26gt%3B%20%22N%2FA%22%20And%20ShVal%20%26lt%3B%26gt%3B%20ShiftYear.Shift_3.Value%20Then%0A%20%20%20%20File_Name%20%3D%20%22Archive%20%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22_%22%20%26amp%3B%20ThisWorkbook.Name%20'Set%20default%20(suggested)%20File%20Name%0A%20%20%20%20Set%20ObFD%20%3D%20Application.FileDialog(msoFileDialogSaveAs)%0A%20%20%20%20With%20ObFD%0A%20%20%20%20%20%20%20%20.Title%20%3D%20%22Archive%20Year(s)%20-%20Personnel%20Tracker%22%0A%20%20%20%20%20%20%20%20.ButtonName%20%3D%20%22A%26amp%3Brchive%22%0A%20%20%20%20%20%20%20%20.InitialFileName%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20File_Name%20'Default%20file%20location%20and%20File%20Name%0A%20%20%20%20%20%20%20%20.FilterIndex%20%3D%201%20'File%20Type%20(.xlsx)%0A%20%20%20%20%20%20%20%20.AllowMultiSelect%20%3D%20False%0A%20%20%20%20%20%20%20%20.InitialView%20%3D%20msoFileDialogViewDetails%0A%20%20%20%20%20%20%20%20.Show%0A%20%20%20%20%20%20%20%20If%20.SelectedItems.count%20%3D%200%20Then%0A%20%20%20%20%20%20%20%20MsgBox%20%22Generation%20and%20archiving%20canceled.%20No%20year(s)%20were%20created%2C%20shifted%2C%20or%20overwritten.%20To%20continue%20generating%20without%20archiving%2C%20uncheck%20the%20%22%22Archive%20%26lt%3B%3D5%20year(s)%20calendar%2Fpersonnel%20data%20externally%20before%20overwriting%22%22%20box%20then%20click%20%22%22Generate%22%22%20again.%22%20_%0A%20%20%20%20%20%20%20%20%2C%20vbExclamation%2C%20%22Year%20Shift%20%26amp%3B%20Creation%20-%20Personnel%20Tracker%22%0A%20%20%20%20%20%20%20%20'%3CMY%20code%3D%22%22%20that%3D%22%22%20turns%3D%22%22%20off%3D%22%22%20macro%3D%22%22%20enhancement%3D%22%22%3E%0A%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20PathAndFile_Name%20%3D%20.SelectedItems(1)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20With%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20%0A'Load%20year%20to%20be%20archived%3A%0A%20%20%20%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22I7%22).Value%20%3D%20Sheet_Archive.Name%0A%20%20%20%20Worksheets(%22Formula%20%26amp%3B%20Code%20Data%22).Range(%22I13%22).Value%20%3D%20%22No%22%0A%20%20%20%20Call%20Load_Year.Load_Year%0A%0A'Copy%20Troop%20to%20Task%20-%20Tracker%20sheet%20into%20new%20workbook%20and%20format%3A%0A%20%20%20%20PleaseWait.Label2.Caption%20%3D%20%22Creating%20%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22%20archive%20file%20...%22%0A%20%20%20%20DoEvents%0A%20%20%20%20File_Name%20%3D%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22%5C%22))%20'Update%20File%20Name%20to%20user's%20input%0A%20%20%20%20ThisWorkbook.Sheets(%22Troop%20to%20Task%20-%20Tracker%22).Copy%0A%20%20%20%20%0A%20%20%20%20If%20PathAndFile_Name%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20File_Name%20Then%20'Error%20handler%0AArchive_Error_Actual%3A%0A%20%20%20%20MsgBox%20%22You%20cannot%20save%20into%20the%20same%20location%20as%20this%20Tracker%2C%20in%20this%20version.%20Please%20select%20a%20different%20file%20location.%22%20_%0A%20%20%20%20%2C%20vbExclamation%2C%20%22Year%20Shift%20%26amp%3B%20Creation%20-%20Personnel%20Tracker%22%0A%20%20%20%20'UPDATE%20MESSAGE%20AND%20FIGURE%20OUT%20WAY%20TO%20FIX%20RUNTIME%20ERROR%20WHEN%20SAVING%20TO%20SAME%20LOCATION%20AS%20THE%20TRACKER!!!%0A%20%20%20%20ActiveWorkbook.Close%20savechanges%3A%3DFalse%0A%20%20%20%20GoTo%20Archive_Error%0A%20%20%20%20End%20If%0A%20%20%20%20On%20Error%20GoTo%20Archive_Error_Actual%0A%20%20%20%20ActiveWorkbook.SaveAs%20fileName%3A%3DPathAndFile_Name%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%20'New%20workbook%20save%20and%20activate%0A%0A%20%20%20%20'%3CALL%20my%3D%22%22%20code%3D%22%22%20that%3D%22%22%20changes%3D%22%22%20the%3D%22%22%20new%3D%22%22%20workbook%3D%22%22%3E%0A%0A%20%20%20%20Excel.Workbooks(File_Name).Activate%0A%20%20%20%20Excel.Workbooks(File_Name).Close%20savechanges%3A%3DTrue%20'New%20workbook%20save%20and%20close%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20End%20If%0A%20%20%20%20End%20Select%0A%20%20%20%20If%20(Sheet_Archive.CodeName%20%3D%20%22Sheet4%22%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet5%22%20_%0A%20%20%20%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet6%22%20Or%20Sheet_Archive.CodeName%20%3D%20%22Sheet7%22)%20_%0A%20%20%20%20And%20ShVal%20%26lt%3B%26gt%3B%20ShiftYear.Shift_3.Value%20Then%0A%20%20%20%20PleaseWait.Label2.Caption%20%3D%20%22%22%20%26amp%3B%20Sheet_Archive.Name%20%26amp%3B%20%22%20archive%20file%20complete%22%0A%20%20%20%20DoEvents%0A%20%20%20%20Else%3A%20PleaseWait.Label2.Caption%20%3D%20%22Initailizing%20archive%20...%22%0A%20%20%20%20DoEvents%3A%20End%20If%0A%20%20%20%20Next%20Sheet_Archive%0AElseIf%20ShiftYear.CheckBox5.Value%20%3D%20False%20Then%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20Unchecked%3A%0A%20%20%20%20'Do%20Nothing%0AEnd%20If%20'Archive%20%26lt%3B%3D5%20year(s)%20data%20externally%20-%20END%0A%0A'%3CCONTINUING%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%3E%3C%2FCONTINUING%3E%3C%2FALL%3E%3C%2FMY%3E%3C%2FA%3E%3C%2FPREVIOUS%3E%3C%2FCODE%3E%3CA%20whole%3D%22%22%20bunch%3D%22%22%20of%3D%22%22%20other%3D%22%22%20checks%3D%22%22%20and%3D%22%22%20code%3D%22%22%20that%3D%22%22%20doesn%3D%22%22%20target%3D%22_blank%22%3E%3C%2FA%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20solution%20to%20this%20is%20much%20appreciated!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3249728%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3261862%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3261862%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40NikolinoDE%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EUpdate%3A%3C%2FSTRONG%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%2C%20the%20variable%20%22PathAndFile_Name%22%20is%20the%20defined%20path%20and%20name%2Ftype%20in%20the%20SaveAs%20dialog.%20For%20example%2C%20is%20%22C%3A%5CUsers%5CMY%20NAME%5CDesktop%5CCUSTOM%20NAME.xlsx%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%2C%20the%20variable%20%22File_Name%22%20is%20just%20the%20defined%20name%2Ftype%20in%20the%20SaveAs%20dialog.%20For%20example%2C%20%22CUSTOM%20NAME.xlsx%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20most%20of%20the%20internet's%20examples%2C%20I%20thought%20the%20%22FileName%3A%3D%22%20was%20to%20include%20the%20full%20path.%20But%2C%20while%20still%20getting%20the%20runtime%20error%201004%20and%20reading%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.workbook.saveas%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.workbook.saveas%3C%2FA%3E%20more%20carefully%2C%20I%20tried%20using%20just%20%22File_Name%22%20instead%20of%20%22PathAndFile_Name%22%20in%20%22ActiveWorkbook.SaveAs%22%20(line%2048%20below).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20know%20how%20or%20why%20but%20I%20%3CU%3E%3CSTRONG%3Esolved%20it%20by%20changing%20%22PathAndFile_Name%22%20to%20just%20%22File_Name%22%3C%2FSTRONG%3E%3C%2FU%3E%2C%20and%20it%20%3CSTRONG%3Ewill%20no%20longer%20produce%20an%20error%20when%20saving%20in%20the%20same%20file%20location%20as%20the%20main%20%22.xlsm%22%20workbook%20and%20still%20works%20with%20other%20user-selected%20file%20locations%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat's%20weird%20is%20using%20the%20full%20path%20in%20%22ActiveWorkbook.SaveAs%20FileName%3A%3D%22%20works%20in%20every%20way%20but%20the%20same%20file%20location%20as%20the%20main%20.xlsm.%20But%2C%20just%20using%20the%20name%20works%20in%20any%20location.%20If%20someone%20understands%20why%20I'd%20love%20to%20know%2C%20but%20regardless%20am%20glad%20it%20works.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20anyone%20looking%20for%20a%20complete%20SaveAs%20code%20(using%20%22Application.FileDialog(msoFileDialogSaveAs)%22)%2C%20feel%20free%20to%20paste%20this%20working%20example%20into%20your%20project%20then%20edit%20as%20needed%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20SaveAs_YourFile()%0A%0A'Set%20then%20launch%20SaveAs%20dialog%20(YOU%20CAN%20EDIT%20THIS%20AS%20NEEDED)%3A%0A%20%20%20%20On%20Error%20GoTo%20SaveAs_Error_Handler%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20%0A%20%20%20%20Dim%20ObFD%20As%20FileDialog%0A%20%20%20%20Dim%20File_Name%20As%20String%0A%20%20%20%20Dim%20PathAndFile_Name%20As%20String%0A%20%20%20%20File_Name%20%3D%20%22YOUR%20DEFAULT%20NAME%22%20'Set%20default%20(suggested)%20File%20Name%0A%20%20%20%20%0A%20%20%20%20Set%20ObFD%20%3D%20Application.FileDialog(msoFileDialogSaveAs)%0A%20%20%20%20With%20ObFD%0A%20%20%20%20%20%20%20%20.Title%20%3D%20%22Save%20As%20-%20YOUR%20PROJECT%20NAME%22%0A%20%20%20%20%20%20%20%20.ButtonName%20%3D%20%22S%26amp%3Bave%22%0A%20%20%20%20%20%20%20%20.InitialFileName%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20File_Name%20'Default%20file%20location%20and%20File%20Name%0A%20%20%20%20%20%20%20%20.FilterIndex%20%3D%201%20'File%20Type%20(.xlsx)%0A%20%20%20%20%20%20%20%20.AllowMultiSelect%20%3D%20False%0A%20%20%20%20%20%20%20%20.InitialView%20%3D%20msoFileDialogViewDetails%0A%20%20%20%20%20%20%20%20.Show%0A%20%20%20%20%20%20%20%20If%20.SelectedItems.count%20%3D%200%20Then%0A%20%20%20%20%20%20%20%20MsgBox%20%22Save%20As%20canceled%22%2C%20vbExclamation%2C%20%22Save%20As%20-%20YOUR%20PROJECT%20NAME%22%0A%20%20%20%20%20%20%20%20Application.ScreenUpdating%20%3D%20True%3A%20Exit%20Sub%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20PathAndFile_Name%20%3D%20.SelectedItems(1)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20With%0A%0A'Verify%20file%20type%20(YOU%20CAN%20EDIT%2FREMOVE%20THIS%20AS%20NEEDED)%3A%0A%20%20%20%20If%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22.%22)%20%2B%201)%20%26lt%3B%26gt%3B%20%22.xlsx%22%20Then%0A%20%20%20%20MsgBox%20%22Note%3A%20You%20can%20only%20save%20as%20an%20%22%22Excel%20Workbook%20(*.xlsx)%22%22%20file%20type.%20The%20file%20type%20will%20be%20changed%20from%20%22%22%22%20_%0A%20%20%20%20%26amp%3B%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22.%22)%20%2B%201)%20%26amp%3B%20%22%22%22%20to%20%22%20%26amp%3B%20%22%22%22.xlsx%22%22.%22%20_%0A%20%20%20%20%2C%20vbExclamation%2C%20%22Save%20As%20-%20YOUR%20PROJECT%20NAME%22%0A%20%20%20%20PathAndFile_Name%20%3D%20Replace(PathAndFile_Name%2C%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20_%0A%20%20%20%20InStrRev(PathAndFile_Name%2C%20%22.%22)%20%2B%201)%2C%20%22.xlsx%22)%3A%20End%20If%20'Check%20if%20file%20type%20is%20not%20.xlsx%0A%20%20%20%20If%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22%5C%22))%20%3D%20%22.xlsx%22%20Then%0A%20%20%20%20PathAndFile_Name%20%3D%20Left(PathAndFile_Name%2C%20InStrRev(PathAndFile_Name%2C%20Application.PathSeparator))%20_%0A%20%20%20%20%26amp%3B%20Replace(File_Name%2C%20%22.xlsm%22%2C%20%22.xlsx%22)%3A%20End%20If%20'Check%20if%20file%20name%20is%20literally%20just%20.xlsx%0A%0A'Copy%20YOUR%20SHEET%20into%20new%20workbook%20then%20edit%20(YOU%20CAN%20EDIT%20THIS%20AS%20NEEDED)%3A%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20File_Name%20%3D%20Right(PathAndFile_Name%2C%20Len(PathAndFile_Name)%20-%20InStrRev(PathAndFile_Name%2C%20%22%5C%22))%20'Update%20File%20Name%20to%20user's%20input%0A%20%20%20%20ThisWorkbook.Sheets(%22%22%20%26amp%3B%20Sheet1.Name%20%26amp%3B%20%22%22).Copy%20'SPECIFY%20THIS%20AS%20NEEDED%0A%20%20%20%20'Note%3A%20The%20Sheets.copy%20is%20what%20creates%20a%20new%20workbook%2C%20thus%20the%20following%20code%20to%20save%20it%20then%20work%20_%0A%20%20%20%20with%20it.%20With%20this%20method%2C%20only%20one%20sheet%20copy%20occurs%20at%20a%20time%20with%20the%20applicable%20year%20loaded%20_%0A%20%20%20%20beforehand.%20%22If%20you%20don't%20specify%20either%20Before%20or%20After%2C%20Microsoft%20Excel%20creates%20a%20new%20workbook%20_%0A%20%20%20%20that%20contains%20the%20copied%20Worksheet%20object.%22%20(https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.worksheet.copy)%0A%20%20%20%20ActiveWorkbook.SaveAs%20fileName%3A%3DFile_Name%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%20'New%20workbook%20save%20and%20activate%0A%20%20%20%20%0A%20%20%20%20With%20Excel.Workbooks(File_Name).Sheets%0A%20%20%20%20'%3CYOUR%20code%3D%22%22%20to%3D%22%22%20edit%3D%22%22%20the%3D%22%22%20new%3D%22%22%20workbook%3D%22%22%20sheet%3D%22%22%20here%3D%22%22%3E%0A%20%20%20%20End%20With%0A%0A%20%20%20%20Excel.Workbooks(File_Name).Activate%0A%20%20%20%20Excel.Workbooks(File_Name).Close%20SaveChanges%3A%3DTrue%20'New%20workbook%20save%20and%20close%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20'Note%3A%20For%20full%20name%20of%20new%20workbook%3A%20%22ObFD.InitialFile_Name%22%0A%20%20%20%20'For%20more%20information%20about%20Excel%20file%20types%3A%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.xlfileformat%0A%20%20%20%20'Original%20Code%3A%20https%3A%2F%2Fstackoverflow.com%2Fquestions%2F60696187%2Fsave-a-new-excel-file-to-a-user-given-path-with-filedialog-msofiledialogsaveas%0A%0A'Error%20handler%20(YOU%20CAN%20EDIT%20THIS%20AS%20NEEDED)%3A%0AIf%201%20%3D%202%20Then%0ASaveAs_Error_Handler%3A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%3A%20Application.DisplayAlerts%20%3D%20True%0A%20%20%20%20MsgBox%20%22An%20unexpected%20error%20occurred%20while%20saving%20%22%20%26amp%3B%20File_Name%20%26amp%3B%20%22!%22%2C%20vbCritical%2C%20%22Save%20As%20-%20YOUR%20PROJECT%20NAME%22%0AElse%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0A%20%20%20%20MsgBox%20File_Name%20%26amp%3B%20%22%20saved%20successfully.%22%2C%20vbInformation%2C%20%22Save%20As%20-%20YOUR%20PROJECT%20NAME%22%0AEnd%20If%0A%0AEnd%20Sub%3C%2FYOUR%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3255783%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3255783%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1140325%22%20target%3D%22_blank%22%3E%40Kendethar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%3EI%20don't%20really%20know%20how%20I%20can%20help%20you%20either.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3E%3CSPAN%3EIf%20none%20of%20the%20specialists%20here%20come%20up%20with%20a%20solution%20suggestion%2C%20take%20a%20look%20here%20as%20well%2C%20maybe%20this%20%22%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%3Fsort%3Dnewest%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAll%20Questions%3C%2FA%3E%22%20will%20help%20you%20further.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%3EThank%20you%20for%20your%20patience%20and%20time.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel-blog%2Fmeet-niko-chatzoudis-excel-forum-contributor%2Fba-p%2F2941385%22%20target%3D%22_blank%22%3ENikolinoDE%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3255684%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3255684%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40NikolinoDE%3C%2FA%3E%2C%3CBR%20%2F%3EP.s.%20ThisWorkbook.Sheets(%22Troop%20to%20Task%20-%20Tracker%22).Copy%20(line%2058)%20is%20what%20creates%20a%20new%20workbook.%3CBR%20%2F%3E%3CBR%20%2F%3EExplained%20better%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.worksheet.copy%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice%2Fvba%2Fapi%2Fexcel.worksheet.copy%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3255683%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3255683%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40NikolinoDE%3C%2FA%3E%2C%3CBR%20%2F%3EI%20did%20use%20Application.DisplayAlerts%20%3D%20False%20(line%2047)%20but%20won't%20prevent%20the%20runtime%20error%20(VBA%20cannot%20execute).%20And%2C%20the%20goal%20is%20to%20save%20the%20new%20file%20as%20a%20.xlsx%2C%20which%20it%20does%20fine%20in%20every%20other%20scenario%2C%20except%20when%20the%20file%20path%20is%20the%20same%20as%20the%20master%20file%20(.xlsm).%20I%20cannot%20wrap%20my%20head%20around%20it.%3CBR%20%2F%3EAnd%20to%20respond%20to%20your%20specific%20response%2C%20I'm%20not%20saving%20the%20file%20the%20macro%20is%20ran%20on%20but%20rather%20creating%20a%20brand%20new%20.xlsx%20then%20saving%20that.%20The%20code%20is%20all%20ran%20from%20a%20.xlsm%2C%20which%20itself%20is%20not%20saved%20or%20changed.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3251335%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3251335%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1140325%22%20target%3D%22_blank%22%3E%40Kendethar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20far%20as%20I%20could%20understand%2C%20your%20file%20is%20equipped%20with%20a%20macro%2C%20so%20it%20cannot%20be%20saved%20in%20xlsx%20without%20an%20error%20message.%20You%20have%20to%20do%20this%20in%20xlsm%20to%20use%20the%20macro%3C%2FP%3E%3CP%3Eor%20if%20you%20really%20want%20to%20save%20it%20in%20xlsx%2C%20turn%20off%20%2F%20save%20%2F%20turn%20on%20the%20error%20message.%3CBR%20%2F%3Ehere%20is%20a%20simple%20pattern%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20SaveAsXlsx()%0A'Save%20the%20file%20without%20macros%20(as%20an%20.xlsx%20file).%0AApplication.DisplayAlerts%20%3D%20False%20'Error%20alerts%20off%0A'here%20with%20direct%20path%20specification%0AActiveWorkbook.SaveAs%20Filename%3A%3DEnviron(%22USERPROFILE%22)%20%26amp%3B%20%22%5CDesktop%5CTestfile.xlsx%22%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FileFormat%3A%3DxlOpenXMLWorkbook%0AApplication.DisplayAlerts%20%3D%20True%20'%20Displays%20error%20messages%0Aend%20sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EI%20hope%20that%20this%20approach%20leads%20to%20the%20cancellation%20of%20the%20message%2C%20I%20haven't%20tried%20it.%3CBR%20%2F%3EAt%20the%20same%20time%2C%20if%20this%20does%20not%20help%20you%2C%20it%20would%20be%20an%20advantage%20to%20know%20about%20the%20Excel%20version%2C%20operating%20system%20and%20storage%20medium.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel-blog%2Fmeet-niko-chatzoudis-excel-forum-contributor%2Fba-p%2F2941385%22%20target%3D%22_blank%22%3ENikolinoDE%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3250749%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3250749%22%20slang%3D%22en-US%22%3EI%20used%20FileFormat%3A%3D51%20instead%20of%20FileFormat%3A%3DxlOpenXMLWorkbook%20and%20I%20still%20get%20the%20error.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3250443%22%20slang%3D%22en-US%22%3ERe%3A%20HELP%20-%20%22Method%20'SaveAs'%20of%20object%20'_Workbook'%20failed%22%20(1004)%20when%20saving%20into%20same%20fil%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3250443%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1140325%22%20target%3D%22_blank%22%3E%40Kendethar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Ftopic%2Ferror-message-when-you-run-a-visual-basic-for-applications-macro-in-excel-method-saveas-of-object-worksheet-failed-376fcbb2-9941-f34d-1aba-ca602903245f%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EError%20message%20when%20you%20run%20a%20Visual%20Basic%20for%20Applications%20macro%20in%20Excel%3A%20%22Method%20'SaveAs'%20of%20object%20'_Worksheet'%20failed%22%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%3EMaybe%20the%20information%20in%20the%20link%20will%20help%20you.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20understanding%20and%20patience%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel-blog%2Fmeet-niko-chatzoudis-excel-forum-contributor%2Fba-p%2F2941385%22%20target%3D%22_blank%22%3ENikolinoDE%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

Hello,

 

     I have code designed to create an archive of my main sheet (as a .xlsx) by copying the sheet > saving the copied sheet in a new workbook > doing things to the sheet within the new workbook > saving and closing new workbook then continuing the rest of my code. 

 

     Everything works as coded except when the user selects (or keeps selected) the same file location, in the SaveAs dialog, that the original file (with the running VBA) is in. It returns a "Method 'SaveAs' of object '_Workbook' failed" error.

 

     I created an "If" check to see if the selected file location from the SaveAs dialog is the same as the file location of the original and was able to create an error handler (avoid the error), but not an error solution. I want to default to the same file location as the original, and regardless I want the user to be able to save into the same file location, especially since that is a very typical thing to do.

 

     Line (59) with error 1004:

ActiveWorkbook.SaveAs fileName:=PathAndFile_Name, FileFormat:=xlOpenXMLWorkbook

     ShiftYear (what code is in) and PleaseWait are userforms, and "Troop to Task - Tracker" is the sheet I'm copying.

 

Code with error:

 

 

 

 

'<PREVIOUS CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

'''Declare variables:
'General:
Dim NewGenYear As Integer, LastGenYear As Integer, year_create_counter As Integer
NewGenYear = 0: LastGenYear = 0: year_create_counter = 0
'Personnel:
Dim cell_person As Range, cell_num As Range
Dim cell_num_default As Range
'Archive:
Dim Sheet_Archive As Worksheet, ShVal As Integer
Dim ObFD As FileDialog
Dim File_Name As String
Dim PathAndFile_Name As String
Dim Shape_Clr As Shape
Dim cell_color_convert As Range

'<A WHOLE BUNCH OF OTHER CHECKS AND CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

'Set then launch SaveAs dialog:
If ShiftYear.CheckBox5.Value = True Then 'Archive <=5 year(s) data externally - Checked:
    For Each Sheet_Archive In ThisWorkbook.Sheets
    Select Case Sheet_Archive.CodeName
    Case Is = "Sheet4", "Sheet5", "Sheet6", "Sheet7"
    ShVal = Sheet_Archive.Name
    If Sheet_Archive.Range("A2").Value <> "N/A" And ShVal <> ShiftYear.Shift_3.Value Then
    File_Name = "Archive " & Sheet_Archive.Name & "_" & ThisWorkbook.Name 'Set default (suggested) File Name
    Set ObFD = Application.FileDialog(msoFileDialogSaveAs)
    With ObFD
        .Title = "Archive Year(s) - Personnel Tracker"
        .ButtonName = "A&rchive"
        .InitialFileName = ThisWorkbook.Path & "\" & File_Name 'Default file location and File Name
        .FilterIndex = 1 'File Type (.xlsx)
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .Show
        If .SelectedItems.count = 0 Then
        MsgBox "Generation and archiving canceled. No year(s) were created, shifted, or overwritten. To continue generating without archiving, uncheck the ""Archive <=5 year(s) calendar/personnel data externally before overwriting"" box then click ""Generate"" again." _
        , vbExclamation, "Year Shift & Creation - Personnel Tracker"
        '<MY CODE THAT TURNS OFF MACRO ENHANCEMENT>
        Exit Sub
        Else
        PathAndFile_Name = .SelectedItems(1)
        End If
    End With
    Application.DisplayAlerts = False
    
'Load year to be archived:
    Worksheets("Formula & Code Data").Range("I7").Value = Sheet_Archive.Name
    Worksheets("Formula & Code Data").Range("I13").Value = "No"
    Call Load_Year.Load_Year

'Copy Troop to Task - Tracker sheet into new workbook and format:
    PleaseWait.Label2.Caption = "Creating " & Sheet_Archive.Name & " archive file ..."
    DoEvents
    File_Name = Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, "\")) 'Update File Name to user's input
    ThisWorkbook.Sheets("Troop to Task - Tracker").Copy

    ActiveWorkbook.SaveAs fileName:=PathAndFile_Name, FileFormat:=xlOpenXMLWorkbook 'New workbook save and activate

    '<ALL MY CODE THAT CHANGES THE NEW WORKBOOK>

    Excel.Workbooks(File_Name).Activate
    Excel.Workbooks(File_Name).Close savechanges:=True 'New workbook save and close
    Application.DisplayAlerts = True
    End If
    End Select
    If (Sheet_Archive.CodeName = "Sheet4" Or Sheet_Archive.CodeName = "Sheet5" _
    Or Sheet_Archive.CodeName = "Sheet6" Or Sheet_Archive.CodeName = "Sheet7") _
    And ShVal <> ShiftYear.Shift_3.Value Then
    PleaseWait.Label2.Caption = "" & Sheet_Archive.Name & " archive file complete"
    DoEvents
    Else: PleaseWait.Label2.Caption = "Initailizing archive ..."
    DoEvents: End If
    Next Sheet_Archive
ElseIf ShiftYear.CheckBox5.Value = False Then 'Archive <=5 year(s) data externally - Unchecked:
    'Do Nothing
End If 'Archive <=5 year(s) data externally - END

'<CONTINUING CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

 

 

 

 

 

Code with error handler:

 

 

 

 

'<PREVIOUS CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

'''Declare variables:
'General:
Dim NewGenYear As Integer, LastGenYear As Integer, year_create_counter As Integer
NewGenYear = 0: LastGenYear = 0: year_create_counter = 0
'Personnel:
Dim cell_person As Range, cell_num As Range
Dim cell_num_default As Range
'Archive:
Dim Sheet_Archive As Worksheet, ShVal As Integer
Dim ObFD As FileDialog
Dim File_Name As String
Dim PathAndFile_Name As String
Dim Shape_Clr As Shape
Dim cell_color_convert As Range

'<A WHOLE BUNCH OF OTHER CHECKS AND CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

'Set then launch SaveAs dialog:
If ShiftYear.CheckBox5.Value = True Then 'Archive <=5 year(s) data externally - Checked:
    For Each Sheet_Archive In ThisWorkbook.Sheets
    Select Case Sheet_Archive.CodeName
    Case Is = "Sheet4", "Sheet5", "Sheet6", "Sheet7"
Archive_Error:
    ShVal = Sheet_Archive.Name
    If Sheet_Archive.Range("A2").Value <> "N/A" And ShVal <> ShiftYear.Shift_3.Value Then
    File_Name = "Archive " & Sheet_Archive.Name & "_" & ThisWorkbook.Name 'Set default (suggested) File Name
    Set ObFD = Application.FileDialog(msoFileDialogSaveAs)
    With ObFD
        .Title = "Archive Year(s) - Personnel Tracker"
        .ButtonName = "A&rchive"
        .InitialFileName = ThisWorkbook.Path & "\" & File_Name 'Default file location and File Name
        .FilterIndex = 1 'File Type (.xlsx)
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .Show
        If .SelectedItems.count = 0 Then
        MsgBox "Generation and archiving canceled. No year(s) were created, shifted, or overwritten. To continue generating without archiving, uncheck the ""Archive <=5 year(s) calendar/personnel data externally before overwriting"" box then click ""Generate"" again." _
        , vbExclamation, "Year Shift & Creation - Personnel Tracker"
        '<MY CODE THAT TURNS OFF MACRO ENHANCEMENT>
        Exit Sub
        Else
        PathAndFile_Name = .SelectedItems(1)
        End If
    End With
    Application.DisplayAlerts = False
    
'Load year to be archived:
    Worksheets("Formula & Code Data").Range("I7").Value = Sheet_Archive.Name
    Worksheets("Formula & Code Data").Range("I13").Value = "No"
    Call Load_Year.Load_Year

'Copy Troop to Task - Tracker sheet into new workbook and format:
    PleaseWait.Label2.Caption = "Creating " & Sheet_Archive.Name & " archive file ..."
    DoEvents
    File_Name = Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, "\")) 'Update File Name to user's input
    ThisWorkbook.Sheets("Troop to Task - Tracker").Copy
    
    If PathAndFile_Name = ThisWorkbook.Path & "\" & File_Name Then 'Error handler
Archive_Error_Actual:
    MsgBox "You cannot save into the same location as this Tracker, in this version. Please select a different file location." _
    , vbExclamation, "Year Shift & Creation - Personnel Tracker"
    'UPDATE MESSAGE AND FIGURE OUT WAY TO FIX RUNTIME ERROR WHEN SAVING TO SAME LOCATION AS THE TRACKER!!!
    ActiveWorkbook.Close savechanges:=False
    GoTo Archive_Error
    End If
    On Error GoTo Archive_Error_Actual
    ActiveWorkbook.SaveAs fileName:=PathAndFile_Name, FileFormat:=xlOpenXMLWorkbook 'New workbook save and activate

    '<ALL MY CODE THAT CHANGES THE NEW WORKBOOK>

    Excel.Workbooks(File_Name).Activate
    Excel.Workbooks(File_Name).Close savechanges:=True 'New workbook save and close
    Application.DisplayAlerts = True
    End If
    End Select
    If (Sheet_Archive.CodeName = "Sheet4" Or Sheet_Archive.CodeName = "Sheet5" _
    Or Sheet_Archive.CodeName = "Sheet6" Or Sheet_Archive.CodeName = "Sheet7") _
    And ShVal <> ShiftYear.Shift_3.Value Then
    PleaseWait.Label2.Caption = "" & Sheet_Archive.Name & " archive file complete"
    DoEvents
    Else: PleaseWait.Label2.Caption = "Initailizing archive ..."
    DoEvents: End If
    Next Sheet_Archive
ElseIf ShiftYear.CheckBox5.Value = False Then 'Archive <=5 year(s) data externally - Unchecked:
    'Do Nothing
End If 'Archive <=5 year(s) data externally - END

'<CONTINUING CODE THAT DOESN'T PERTAIN TO THE SAVEAS ISSUE>

 

 

 

 

Any solution to this is much appreciated!

7 Replies

@Kendethar 

 

Error message when you run a Visual Basic for Applications macro in Excel: "Method 'SaveAs' of objec...

Maybe the information in the link will help you.

 

 

Thank you for your understanding and patience

 

NikolinoDE

I know I don't know anything (Socrates)

I used FileFormat:=51 instead of FileFormat:=xlOpenXMLWorkbook and I still get the error.

@Kendethar 

As far as I could understand, your file is equipped with a macro, so it cannot be saved in xlsx without an error message. You have to do this in xlsm to use the macro

or if you really want to save it in xlsx, turn off / save / turn on the error message.
here is a simple pattern

Sub SaveAsXlsx()
'Save the file without macros (as an .xlsx file).
Application.DisplayAlerts = False 'Error alerts off
'here with direct path specification
ActiveWorkbook.SaveAs Filename:=Environ("USERPROFILE") & "\Desktop\Testfile.xlsx", _
                       FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True ' Displays error messages
end sub

I hope that this approach leads to the cancellation of the message, I haven't tried it.
At the same time, if this does not help you, it would be an advantage to know about the Excel version, operating system and storage medium.

 

NikolinoDE

I know I don't know anything (Socrates)

@NikolinoDE,
I did use Application.DisplayAlerts = False (line 47) but won't prevent the runtime error (VBA cannot execute). And, the goal is to save the new file as a .xlsx, which it does fine in every other scenario, except when the file path is the same as the master file (.xlsm). I cannot wrap my head around it.
And to respond to your specific response, I'm not saving the file the macro is ran on but rather creating a brand new .xlsx then saving that. The code is all ran from a .xlsm, which itself is not saved or changed.
@NikolinoDE,
P.s. ThisWorkbook.Sheets("Troop to Task - Tracker").Copy (line 58) is what creates a new workbook.

Explained better: https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.copy

@Kendethar 

I don't really know how I can help you either. If none of the specialists here come up with a solution suggestion, take a look here as well, maybe this "All Questions" will help you further.

 

Thank you for your patience and time.

 

NikolinoDE

I know I don't know anything (Socrates)

best response confirmed by Kendethar (Contributor)
Solution

@NikolinoDE 

 

Update: 

 

So, the variable "PathAndFile_Name" is the defined path and name/type in the SaveAs dialog. For example, is "C:\Users\MY NAME\Desktop\CUSTOM NAME.xlsx"

 

And, the variable "File_Name" is just the defined name/type in the SaveAs dialog. For example, "CUSTOM NAME.xlsx"

 

Based on most of the internet's examples, I thought the "FileName:=" was to include the full path. But, while still getting the runtime error 1004 and reading https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas more carefully, I tried using just "File_Name" instead of "PathAndFile_Name" in "ActiveWorkbook.SaveAs" (line 48 below).

 

I don't know how or why but I solved it by changing "PathAndFile_Name" to just "File_Name", and it will no longer produce an error when saving in the same file location as the main ".xlsm" workbook and still works with other user-selected file locations.

 

What's weird is using the full path in "ActiveWorkbook.SaveAs FileName:=" works in every way but the same file location as the main .xlsm. But, just using the name works in any location. If someone understands why I'd love to know, but regardless am glad it works.

 

For anyone looking for a complete SaveAs code (using "Application.FileDialog(msoFileDialogSaveAs)"), feel free to paste this working example into your project then edit as needed:

Sub SaveAs_YourFile()

'Set then launch SaveAs dialog (YOU CAN EDIT THIS AS NEEDED):
    On Error GoTo SaveAs_Error_Handler
    Application.ScreenUpdating = False
    
    Dim ObFD As FileDialog
    Dim File_Name As String
    Dim PathAndFile_Name As String
    File_Name = "YOUR DEFAULT NAME" 'Set default (suggested) File Name
    
    Set ObFD = Application.FileDialog(msoFileDialogSaveAs)
    With ObFD
        .Title = "Save As - YOUR PROJECT NAME"
        .ButtonName = "S&ave"
        .InitialFileName = ThisWorkbook.Path & "\" & File_Name 'Default file location and File Name
        .FilterIndex = 1 'File Type (.xlsx)
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .Show
        If .SelectedItems.count = 0 Then
        MsgBox "Save As canceled", vbExclamation, "Save As - YOUR PROJECT NAME"
        Application.ScreenUpdating = True: Exit Sub
        Else
        PathAndFile_Name = .SelectedItems(1)
        End If
    End With

'Verify file type (YOU CAN EDIT/REMOVE THIS AS NEEDED):
    If Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, ".") + 1) <> ".xlsx" Then
    MsgBox "Note: You can only save as an ""Excel Workbook (*.xlsx)"" file type. The file type will be changed from """ _
    & Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, ".") + 1) & """ to " & """.xlsx""." _
    , vbExclamation, "Save As - YOUR PROJECT NAME"
    PathAndFile_Name = Replace(PathAndFile_Name, Right(PathAndFile_Name, Len(PathAndFile_Name) - _
    InStrRev(PathAndFile_Name, ".") + 1), ".xlsx"): End If 'Check if file type is not .xlsx
    If Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, "\")) = ".xlsx" Then
    PathAndFile_Name = Left(PathAndFile_Name, InStrRev(PathAndFile_Name, Application.PathSeparator)) _
    & Replace(File_Name, ".xlsm", ".xlsx"): End If 'Check if file name is literally just .xlsx

'Copy YOUR SHEET into new workbook then edit (YOU CAN EDIT THIS AS NEEDED):
    Application.DisplayAlerts = False
    File_Name = Right(PathAndFile_Name, Len(PathAndFile_Name) - InStrRev(PathAndFile_Name, "\")) 'Update File Name to user's input
    ThisWorkbook.Sheets("" & Sheet1.Name & "").Copy 'SPECIFY THIS AS NEEDED
    'Note: The Sheets.copy is what creates a new workbook, thus the following code to save it then work _
    with it. With this method, only one sheet copy occurs at a time with the applicable year loaded _
    beforehand. "If you don't specify either Before or After, Microsoft Excel creates a new workbook _
    that contains the copied Worksheet object." (https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.copy)
    ActiveWorkbook.SaveAs fileName:=File_Name, FileFormat:=xlOpenXMLWorkbook 'New workbook save and activate
    
    With Excel.Workbooks(File_Name).Sheets
    '<YOUR CODE TO EDIT THE NEW WORKBOOK SHEET HERE>
    End With

    Excel.Workbooks(File_Name).Activate
    Excel.Workbooks(File_Name).Close SaveChanges:=True 'New workbook save and close
    Application.DisplayAlerts = True
    'Note: For full name of new workbook: "ObFD.InitialFile_Name"
    'For more information about Excel file types: https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
    'Original Code: https://stackoverflow.com/questions/60696187/save-a-new-excel-file-to-a-user-given-path-with-filedialog-msofiledialogsaveas

'Error handler (YOU CAN EDIT THIS AS NEEDED):
If 1 = 2 Then
SaveAs_Error_Handler:
    Application.ScreenUpdating = True: Application.DisplayAlerts = True
    MsgBox "An unexpected error occurred while saving " & File_Name & "!", vbCritical, "Save As - YOUR PROJECT NAME"
Else
    Application.ScreenUpdating = True
    MsgBox File_Name & " saved successfully.", vbInformation, "Save As - YOUR PROJECT NAME"
End If

End Sub