PDA

View Full Version : Runtime 1004 Error SaveAs Workbook Failed



AIM
09-03-2018, 06:18 AM
Hi,

I'm quite new in vba and I was hoping someone could help me.
I have the following code, that saves a copy of a sheet "Loading Form" using as a name a value in cell C7:

Sub SaveITWithNewName()
Dim NewFN As Variant

If Range("C36").Value <= 0.8 Then
MsgBox "Truck not full"
Else
ActiveSheet.Copy
NewFN = "Path to the folder" & Range("C7").Value & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
NextLoading
End If
End Sub

I keep having an error at line "ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook" although it has worked before and suddenly started with this error...

Can someone advise?
Thanks

Paul_Hossler
09-03-2018, 07:27 AM
If (for example) C7 contains "BillSmith" then




NewFN = "Path to the folder" & Range("C7").Value & ".xlsx"


makes NewFN =
Path to the folderBillSmith.xlsx


But I suspect that you deleted the parameter name in bold below during editing




ActiveWorkbook.SaveAs FileName:=NewFN, FileFormat:=xlOpenXMLWorkbook

AIM
09-03-2018, 11:56 PM
If (for example) C7 contains "BillSmith" then




NewFN = "Path to the folder" & Range("C7").Value & ".xlsx"



makes NewFN =
Path to the folderBillSmith.xlsx


But I suspect that you deleted the parameter name in bold below during editing




ActiveWorkbook.SaveAs FileName:=NewFN, FileFormat:=xlOpenXMLWorkbook




Hi Paul,

Thanks for the advise.

"makes NewFN =Path to the folderBillSmith.xlsx" it's indeed not what I need. This file is supposed to be a template for a user to fill in, and save a copy of it using as file name, the value the user inputs in cell C7.
I've add the parameter name but I still get the same error, which I assume comes from your explanation of the folder's path.

Paul_Hossler
09-04-2018, 05:28 AM
Since most users can't type, or might have a hard time entering a correct folder name, maybe allowing them to pick their folder would work better



Option Explicit
Sub test()
MsgBox GetFolderFromUser
End Sub

'returns file name as string, vbnullstring if canceled
Function GetFolderFromUser() As String
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ThisWorkbook.Path
.Title = "Select Folder"
If .Show = -1 Then
GetFolderFromUser = .SelectedItems(1)
Else
GetFolderFromUser = vbNullString
End If
End With
End Function