I'm trying to get the 'Save As' dialogue to prompt with the inputted information on a userform and save as a new file. Everything looks like it works and even has the saving/loading icon when I click 'save' but no actual saved file is saved in my folder. Here is my 'Save As' code:
Dim IntialName As String
Dim fileSaveName As Variant
InitialName = Range("d1") & "_" & Range("j1") & "_" & Range("p1")
fileSaveName = Application.GetSaveAsFileName(InitialFileName:=InitialName, _
fileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
Add
ActiveWorkbook.SaveAs fileSaveName, xlFileFormat.xlOpenXMLWorkbookMacroEnabled
after your code to actually save it.
All your code currently does is give you a string so you can save it using that name. You have to do the actual saving yourself.
Related
I found code online that opens a Save As dialog to a location on a drive.
When you click "save" the file does not save.
Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
Exit Sub
End If
You have to actually explicitly tell Excel to save the workbook.
Sub Mac2()
Dim varResult As Variant
Dim ActBook As Workbook
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
Exit Sub
End If
End Sub
Using the GetSaveAsFilename only gets the path of the file to save, whereas the SaveAs method actually saves the workbook.
Upon some consideration, I might suggest using the SaveCopyAs method instead of simply SaveAs. As the name suggests, this will leave your original workbook in tact and save off a copy. To do this is a rather simply modification.
You would replace
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
With
ActiveWorkbook.SaveCopyAs Filename:=varResult
One final consideration I would add is that if you save your macro-enabled workbook as a .xlsx (either by SaveAs or the SaveCopyAs) then you will lose the macros, either in your original workbook if you use SaveAs or in the copy that is saved if you use SaveCopyAs. I would consider saving the file as a .xlsm instead, if you need macros to be available.
I prefer to use the shortest code:
Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\")
It's the standard Excel save dialog.
It has several parameters (not named), you may need them:
Dim strFilename As String: strFilename = "report1"
Dim strFolder As String: strFolder = "C:\temp\" 'initial directory - NOTE: Only works if file has not yet been saved!
Dim xlfFileFormat As XlFileFormat: xlfFileFormat = XlFileFormat.xlOpenXMLWorkbook 'or replace by other XlFileFormat
Dim strPassword As String: 'strPassword = "password" 'The password with which to protect the file - if any
Dim booBackup As Boolean: 'booBackup = True '(Whether to create a backup of the file.)
Dim strWriteReservationPassword As String: 'strWriteReservationPassword = "password2" ' (The write-reservation password of the file.)
Dim booReadOnlyRecommendation As Boolean: booReadOnlyRecommendation = False '(Whether to recommend to the user that the file be opened in read-only mode.)
Dim booWorkbookSaved As Boolean ' true if file saved, false if dialog canceled
If Len(strFolder) > 0 Then ChDir strFolder
booWorkbookSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=strFilename, Arg2:=xlfFileFormat, Arg3:=strPassword, _
Arg4:=booBackup, Arg5:=strWriteReservationPassword, Arg6:=booReadOnlyRecommendation)
I regularly run a macro in Excel 2010 to loop through a number of .xls files, copying the data from each into a .xlsm file and then saving as a .csv. When I save the csv, however, the document when I next open it has been truncated to c.65,000 rows. I know that this is about the limit of an xls document, but I don't understand why it would happen when I save an xlsm as a csv.
I'm splitting the files into batches as a workaround, but my inability to fix this annoys me every time. Any ideas? (Or if I've totally misdiagnosed the issue, that would be useful to know as well so I can think about what else might be happening).
This is the extract of the code I'm using to save the document:
Sub save_tracker_csv()
Dim today_date As String
today_date = Format(Now(), "ddmmyyyy")
Dim wb As Workbook: Set wb = ThisWorkbook
wb.Sheets(1).Activate
Dim batch As Integer
batch = Application.InputBox("Enter a batch number")
--save copy for archiving
Dim strFilename As String: strFilename = _
"N:\Operations\Practice exports\CSV\" & today_date & "_" & batch & ".csv"
wb.SaveAs Filename:=strFilename, FileFormat:=xlCSV, local:=True
--copy for database
strFilename = _
"N:\Operations\Practice exports\CSV\import_tbl_" & batch & ".csv"
wb.SaveAs Filename:=strFilename, FileFormat:=xlCSV, local:=True
MsgBox "Your file has been saved"
Set wb = Nothing
End sub
Is there anything I'm missing here? Thanks!
I have a long macro that has 10,000 row files. At the end I try to fileSaveName. It crashes all the time if I just run the macro. I can't trace what is wrong because if I step through I get "Can't enter break mode" anywhere near the save code. Same thing happens if I "run to cursor". And then when I click "Continue" the macro runs to the end just fine.
When I put in a "Debug.Assert" break, the macro runs fine after clicking "Continue".
'Get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'Create default name for saving file
strFile = QuoteNo & " - OEM List"
strPathFile = strPath & strFile
'Save now with Message Box
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=strFile, FileFilter:="Excel workbook (*.xlsx), *.xlsx", Title:="Save with this file name or Create a new name")
If fileSaveName <> False Then
ActiveWorkbook.SaveAs Filename:=fileSaveName
End If
Just step through the file reformatting and filtering and save the file without breaking.
Essentially I have the following code stating what cells I would like to copy over into another workbook:
Application.CopyObjectsWithCells = False
ActiveSheet.Copy
Range("A1:T40").Copy
Range("A1:T40").PasteSpecial Paste:=xlPasteValues
Application.CopyObjectsWithCells = True
The VBA script automatically opens a new workbook with all of the data pasted from the other workbook.
But now I would also like it to prompt the user with a save window as soon as the new workbook opens, how can this be done?
UPDATE:
The following code prompts the user to save the excel workbook, what can I add to have it save to a specific file with a specific name?
fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Excel Workbooks (*.xlsx*),*.xlsx*")
If fileSaveName <> False Then
MsgBox "Save as " & fileSaveName
End If
UPDATE 2
Dim path As String
Dim filename1 As String
Application.CopyObjectsWithCells = False
ActiveSheet.Copy
Range("A2:T40").Copy
Range("A2:T40").PasteSpecial Paste:=xlPasteValues
Application.CopyObjectsWithCells = True
path = "C:\Users\jmills\Documents\Report\"
filename1 = Range("H1")
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs Filename:=path & filename1 & ".xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
How can I keep the save as prompt up? I would like it to allow me to edit the name before it saves
Currently with the code above it saves automatically with whatever is written in cell H1
You can trigger the save filename dialog with Application.SaveAsFileName. Here is more info on what you can do with it: https://learn.microsoft.com/en-us/office/vba/api/excel.application.getsaveasfilename
The output of this is a path were you can save the workbook to.
So afterwards, you need to use something like ActiveWorkbook.SaveAs Filename:=[output from application.saveasfilename]
Like this:
path = "C:\Users\jmills\Documents\Report\"
fileSaveName = Application.GetSaveAsFilename( initialFilename:=path & "myworkbook", _
fileFilter:="Excel Workbooks (*.xlsx*),*.xlsx*")
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlOpenXMLWorkbook
Where you can change myworkbook to whatever you want. The user can change that as well in the filedialog if they want.
I found code online that opens a Save As dialog to a location on a drive.
When you click "save" the file does not save.
Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
Exit Sub
End If
You have to actually explicitly tell Excel to save the workbook.
Sub Mac2()
Dim varResult As Variant
Dim ActBook As Workbook
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
Exit Sub
End If
End Sub
Using the GetSaveAsFilename only gets the path of the file to save, whereas the SaveAs method actually saves the workbook.
Upon some consideration, I might suggest using the SaveCopyAs method instead of simply SaveAs. As the name suggests, this will leave your original workbook in tact and save off a copy. To do this is a rather simply modification.
You would replace
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
With
ActiveWorkbook.SaveCopyAs Filename:=varResult
One final consideration I would add is that if you save your macro-enabled workbook as a .xlsx (either by SaveAs or the SaveCopyAs) then you will lose the macros, either in your original workbook if you use SaveAs or in the copy that is saved if you use SaveCopyAs. I would consider saving the file as a .xlsm instead, if you need macros to be available.
I prefer to use the shortest code:
Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\")
It's the standard Excel save dialog.
It has several parameters (not named), you may need them:
Dim strFilename As String: strFilename = "report1"
Dim strFolder As String: strFolder = "C:\temp\" 'initial directory - NOTE: Only works if file has not yet been saved!
Dim xlfFileFormat As XlFileFormat: xlfFileFormat = XlFileFormat.xlOpenXMLWorkbook 'or replace by other XlFileFormat
Dim strPassword As String: 'strPassword = "password" 'The password with which to protect the file - if any
Dim booBackup As Boolean: 'booBackup = True '(Whether to create a backup of the file.)
Dim strWriteReservationPassword As String: 'strWriteReservationPassword = "password2" ' (The write-reservation password of the file.)
Dim booReadOnlyRecommendation As Boolean: booReadOnlyRecommendation = False '(Whether to recommend to the user that the file be opened in read-only mode.)
Dim booWorkbookSaved As Boolean ' true if file saved, false if dialog canceled
If Len(strFolder) > 0 Then ChDir strFolder
booWorkbookSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=strFilename, Arg2:=xlfFileFormat, Arg3:=strPassword, _
Arg4:=booBackup, Arg5:=strWriteReservationPassword, Arg6:=booReadOnlyRecommendation)