Check if file is saved.

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
I have the following sub to save my excel file as a binary workbook. It works great, I just want to add that it should check if my excel file is actually saved already, and if yes it should just save the current file and not save a copy.
I am looking to replace the default save button on the quickaccess toolbar, I find the method used there annoying and long winding...
VBA Code:
Sub SaveBinary()

    Dim fname As Variant
    Dim FileFormatValue As Long
     
        fname = Application.GetSaveAsFilename(InitialFileName:=ActiveCell, filefilter:= _
            " Excel Macro Free Workbook (*.xlsx), *.xlsx," & _
            " Excel Macro Enabled Workbook (*.xlsm), *.xlsm," & _
            " Excel 2000-2003 Workbook (*.xls), *.xls," & _
            " Excel Binary Workbook (*.xlsb), *.xlsb", _
            FilterIndex:=4, Title:="Save as xlsb")

        'Find the correct FileFormat that match the choice in the "Save as type" list
            Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
            Case "xls": FileFormatValue = 56
            Case "xlsx": FileFormatValue = 51
            Case "xlsm": FileFormatValue = 52
            Case "xlsb": FileFormatValue = 50
            Case Else: FileFormatValue = 0
            End Select
         
         If fname = False Then
         Exit Sub
         End If
         
           ActiveWorkbook.SaveAs fname, FileFormat:=FileFormatValue, CreateBackup:=False
           ActiveWorkbook.Save
           
End Sub
 
VBA Code:
change
       MsgBox "Original " & IIf(b, "only ", "and copy ") & "Saved", vbInformation, "Result Saving"
  into
       if not b then MsgBox "Original and copy Saved", vbInformation, "Result Saving"
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
you don't want that whole "getsaveas"-story ?
Just check of changes since last "Save" ?
VBA Code:
If Not ActiveWorkbook.Saved Then ActiveWorkbook.Save
 
Upvote 0
I tried that, but if the workbook has never been saved yet it will run the save, but I will be unable to change its title or choose a location and I also did not figure out where it actually saves the file to.
If you read my post from yesterday at 1:54 pm, I have an amateurish solution there. I simply need a way to know if this workbook has ever been saved. When I use Mid(ActiveWorkbook.FullName, InStr(1, ActiveWorkbook.FullName, "\"), 1) I can tell vba to just save and skip the '"getsaveas"-story' if it finds a slash.
I had to put in error handling so the code should not get stuck if it doesn't find a slash and continue to the '"getsaveas"-story'.
I'm sure there is a better solution I just dont know it.
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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