vba script to save workbook overwrites entered filename - excel

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:
popup question after pressing ok/save:
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?

You didn't include a file name in the saveas line:
ActiveWorkbook.SaveAs filename:=workbook_name, WriteResPassword:="TM", FileFormat:=50

Related

VBA Ctrl+S add date and time to current save directory

I am looking to create a code that will activate upon a user pushing Ctrl+S on VBA.
I have created a workbook that uses macros so I need to overwrite the Ctrl+S function so that when a user saves the file it doesn't overwrite the previous workbook as the data in it will be lost.
so far I have:
Sub Save()
dim path as string
dim time as string
dim fname as string
time = format(Now(), "yyyymmdd\_hhmm")
path = application.thisworkbook.path
fname = path & "\Title" & time
thisworkbook.saveas filename:= fname, _
fileformat:=xlopenxmlworkbookmacroenabled
end sub
How do I make it so that this code replaces Ctrl+S to prevent people from accidentally running the macros then saving and overwriting potentially important data?
Is it possible to only have this Ctrl+S only apply to the workbook the code is written in?
My apologies if this has already been asked.

Save Copy as CSV MSDOS (Without closing/changing workbooks)

I have a macro that saves the current workbook as CSV MSDOS format.
Here's my code:
Sub Save_CSV()
Dim Location, FileName As String
Location = "C:\Users\myawesomename\OneDrive\Desktop\GM MRP\"
FileName = Left(ActiveWorkbook.name, Len(ActiveWorkbook.name) - 5)
ActiveWorkbook.SaveAs FileName:= _
Location & FileName & ".csv", FileFormat:= _
xlCSVMSDOS, CreateBackup:=False
ActiveWorkbook.Save
End Sub
After I use this macro, I'm no longer working on the xlsv. Rather, I'm working on the CSV version with all the sheets still present. If I close the workbook without saving it, I can then open the CSV file and only the first sheet is present. It's fine that only the first sheet is present but I want it to save a separate CSV file (with the first sheet only present) while continuing to work on the XLSX file without opening the CSV at all. I'm not trying to save each sheet as a separate file.
I tried several things including changing "Activeworkbook.SaveAs" to "Activeworkbook.savecopyas" but I couldn't achieve the desired result.
Thank you,

vba routine to save workbook does not have desired result

I have a workbook which is updated every 4 weeks with new data. When updated, it has to be saved with a specific name and with specific options. I already had a script which saves workbook to a new file for me so I used that script and modified it.
Sub save_workbook_name()
Dim workbook_Name As Variant
Dim location As String
location = "N:\IRi\"
workbook_Name = Application.GetSaveAsFilename
If workbook_Name <> False Then
ActiveWorkbook.SaveAs Filename:=Workbook.Name, WriteResPassword:="TM", FileFormat:=50
End If
End Sub
When I use this code and I press the button, a popup screen appears asking me how I want to save the file:
But there is not file format being set. The password for opening the file is set I noticed when opening the saved file. I know for myself that I have to add the .xslb extension when saving the file but I am not sure about any colleague whom also works with this file.
When I enter a filename and extension, I get an error:
error 424: object needed
my wished regarding to the options for saving:
filetype has to be set to .xlsb
to prevent the saved copy from being updated, I want it to be saved with password protection for opening
How can I make the routine to already add the .xlsb extension so only the file names has to be entered?
edit: with the answer from Marcucciboy2 I changed the script to:
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\")
If workbook_Name <> False Then
ActiveWorkbook.SaveAs WriteResPassword:="TM", FileFormat:=50
End If
End Sub
And now it works perfectly for saving.
Additional question with regarding to this script and the entered name is posted in a new question:
vba script to save workbook overwrites entered filename
I think the issue might be that you're not filtering the filename that you receive from GetSaveAsFilename, so try:
Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb")

msoFileDialogSaveAs not writing file Excel VBA

Using the following code to copy some data on a master sheet, then add a new workbook, then paste the data. I then need to prompt using the msoFileDialogSaveAs because I need the user to be able to select different file types each time.
The problem is that when the box comes up to save the file, I can type a name then hit save, but it doesn't actually write the file.
Public Sub ArchiveSheet()
Dim NewBook As Workbook
Dim CopyRange As Range
Set CopyRange = ActiveSheet.UsedRange
Dim lngCount As Long
CopyRange.Cells.Copy
Set NewBook = Workbooks.Add
Range("A1").PasteSpecial Paste:=xlPasteValues
With NewBook
.Title = "Archive"
End With
With Application.FileDialog(msoFileDialogSaveAs)
.Show
End With
Application.CutCopyMode = False
End Sub
You are currently asking to user to indicate where he/she wants to save the file. But you are not using it. You need to save the return string like so:
dim strFileSelected as String
strFileSelected = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Save Excel file...")
Afterwards you can check if the user actually gave you a path and file name to save it to:
If strFileSelected = "False" Then
MsgBox "You have cancelled"
else
MsgBox "Saving file here:" & chr(10) & strFileSelected
ThisWorkbook.SaveAs Filename:=strFileSelected, FileFormat:=xlWorkbookNormal
End If
Note that you cannot save all Excel files using any kind of file extension. Example: if you have an open XML format Excel file open and try to save it using the .xls extension then you'll probably get an error message and you'll loose any kind of VBA code attached to the file (if you ignore the error message).
In short: you might want to elaborate on the above solution in order to make sure that the file format matches the selected extension using Debug.Print ThisWorkbook.FileFormat.
I know this is old but I had this issue today, have not seen this answer anywhere...
And the solution was very simply to add the .Execute line, as below.
Now the user hits Save (or Enter) and the selection executes.
With Application.FileDialog(msoFileDialogSaveAs)
.Show
.Execute
End With
My first post here, sorry about my obvious lack of skills. I am not an advanced user.
Thanks.

Save a worksheet with form Save As location in Excel VBA

Well, I am not a developer but an regular an Excel user try to automate some process while working so please understand if my question is of simple to do.
I just want to a macro that can when I run the macro, it will allow me to
Browse the computer so I can save the file in the direction and file name I like
If possible
Default location is the location of the workbook, just to save time
Thank you
Try below code :
Sub SaveAsDailog()
Application.DisplayAlerts = False
Dim filePath As Variant
filePath = Application.GetSaveAsFilename("abcd.xlsx", "Excel Files (*.xlsx), *.xlsx")
If VarType(filePath) = vbString Then
ActiveWorkbook.SaveAs Filename:=filePath
End If
Application.DisplayAlerts = True
End Sub

Resources