Save code not working correctly

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
What I am trying to achieve is this
When I run the code I want it to open the file path & folder named in the in cell (“AE8”) and name the workbook from the value in cell (“AE10”). This is working fine
Then automatically save the file with a msgbox saying saved.
If file path & folder named in the in cell (“AE8”) does not exist I want it to go to InvalidDirectory
Then open up the root folder named in the cell("AB8") so the appropriate folder can be selected and save into that folder manually.

But it will not automatically save the workbook it just opens the window of the folder where I want to save the workbook waiting for me to save it manually, when I press the save button it will save OK, and the msgbox opens up just fine, but then the code goes on and picks up the root folder from InvalidDirectory: ChDir Range("AB8").Value
And opens a second window so I can select a folder asking to manually save again
The other issue is even when I have saved the file the top part of the code where I just want it to save as normal does not work it runs the code again rather than just saving.
VBA Code:
If BeenSaved Then
ThisWorkbook.Save
Any help is appreciated
Full code below
VBA Code:
Sub Save()
   Static BeenSaved As Boolean
   Dim mySaveFile As Variant

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

   If BeenSaved Then
      ThisWorkbook.Save
   Else
'     Changing drive letter
      ChDrive "L:\"
'     Changing directory
      ChDir Range("AE8").Value
      On Error GoTo InvalidDirectory
'     Prompt for new file location
      mySaveFile = Application.GetSaveAsFilename(Range("AE10").Text & ".xlsm", "Microsoft Excel Workbook (*.xlsm), *.xlsm")
      If mySaveFile = False Then Exit Sub

        ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'     Set BeenSaved flag TRUE so will just do a Save next time
      If Err = 0 Then BeenSaved = True
          
    ActiveWorkbook.Save
    MsgBox "File Saved!"
   
   End If
   
InvalidDirectory:
       ChDir Range("AB8").Value
'     Prompt for new file location
      mySaveFile = Application.GetSaveAsFilename(Range("AE10").Text & ".xlsm", "Microsoft Excel Workbook (*.xlsm), *.xlsm")
      If mySaveFile = False Then Exit Sub

    ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
    MsgBox "File Saved!"

Application.ScreenUpdating = True
Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Apologies
I was really struggling with this but as soon as I posted this I had a “what if” moment and got it sorted
Please see full code below in case it helps anyone else
VBA Code:
Sub Save()
   Static BeenSaved As Boolean
   Dim mySaveFile As Variant

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

   If BeenSaved Then
      ThisWorkbook.Save
   Else
   On Error GoTo InvalidDirectory
'     Changing drive letter
      ChDrive "L:\"
'     Changing directory
      ChDir Range("AE8").Value
      
'     Prompt for new file location
      'mySaveFile = Application.GetSaveAsFilename(Range("AE10").Text & ".xlsm", "Microsoft Excel Workbook (*.xlsm), *.xlsm")
      mySaveFile = (Range("AE10").Text & ".xlsm")
      If mySaveFile = False Then Exit Sub

      'On Error Resume Next
        ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'     Set BeenSaved flag TRUE so will just do a Save next time
      If Err = 0 Then BeenSaved = True
          
    ActiveWorkbook.Save
    MsgBox "File Saved!"
   
   End If
   
Application.ScreenUpdating = True
Application.DisplayAlerts = True

   Exit Sub
   
InvalidDirectory:
       ChDir Range("AB8").Value
'     Prompt for new file location
      mySaveFile = Application.GetSaveAsFilename(Range("AE10").Text & ".xlsm", "Microsoft Excel Workbook (*.xlsm), *.xlsm")
      If mySaveFile = False Then Exit Sub

    ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
    MsgBox "File Saved!"

Application.ScreenUpdating = True
Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
Solution
Why are you saving the same workbook twice ?
Rich (BB code):
    ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
 
Upvote 0
Why are you saving the same workbook twice ?
Rich (BB code):
    ActiveWorkbook.SaveAs Filename:=mySaveFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
Hi Alex
Thanks for that, I was saving it twice because I am an am
 
Upvote 0
Hi Alex
Thanks for that, I was saving it twice because I am an amateur and didn’t notice. I will remove the redundant line
 
Upvote 0
Glad you were able to figure out how to get is working and thank you for providing an update.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top