Search code examples
excelvbasavesave-as

Button to show Save As dialog, then save to a set location


I've been stuck on this for some time now, help would be greatly appreciated.

I've got this concept working when the workbook is closed, but all it does is save to the location specified. Now, I would like to adapt it so once a button is pressed, it will ask the user to save to a location, then once saved, excel will save it to another location of my choosing.

I keep getting an error message "Argument not optional" when the button is pressed.

Thank you.

Private Sub Save(Cancel As Boolean)
  
NameOfWorkbook = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))

Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
Cells(2, 1) = varResult

MyMsg = NameOfWorkbook + " " & "saved to return note folder"

MsgBox MyMsg

'Create and assign variables
Dim saveLocation As String
saveLocation = "S:\Office information\Returns\Return Notes\" + NameOfWorkbook

'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=saveLocation

End If
End Sub

Solution

  • You dont use the sub argument so, as Siddharth pointed out, you should correct Private Sub Save().

    Furthermore, Save is a reserved word in excel, so I wouldn't use it as a Sub name.