vba script to save workbook overwrites entered fil

2019-09-05 02:48发布

With the below entered script I want my workbook to be saved with the name entered by the user, say

Sub save_workbook_name()
Dim workbook_Name As Variant
Dim location As String

workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="N:\IRi\Periode Rapportage Px")

If workbook_Name <> False Then

    ActiveWorkbook.SaveAs WriteResPassword:="TM", FileFormat:=50

End If
End Sub

But when I now try to save the file with a different name, lets say with the X replaced by 9, it turns out the script only allows to save with the same file name as the original filename. So I entered Periode Rapportage P9(.xlsb) and then Excel saves the file with filename Periode Rapportage Zelfzorg v2.xlsb.

Any clues on why this happens? The original file is saved with a password to write security.

Entered desired filename:

enter image description here

popup question after pressing ok/save:

enter image description here

Apparently the entered desired filename was changed back to its original filename.

Is there any way to solve this and make the script save the file with the name entered by the user?

1条回答
不美不萌又怎样
2楼-- · 2019-09-05 03:32

You didn't include a file name in the saveas line:

ActiveWorkbook.SaveAs filename:=workbook_name, WriteResPassword:="TM", FileFormat:=50
查看更多
登录 后发表回答