Consulting

Results 1 to 4 of 4

Thread: Runtime 1004 Error SaveAs Workbook Failed

  1. #1

    Runtime 1004 Error SaveAs Workbook Failed

    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

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    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.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •