SaveAs Runtime Error 1004 issue

Jgordon

New Member
Joined
Apr 4, 2018
Messages
11
Hi Guys,

I'm sometimes getting a runtime error on the "ActiveWorkbook.SaveAs" line
Other times it works like a dream. I'm confused...


Rich (BB code):
Sub TestForDirAndSave()
  If MsgBox("Are you sure?", vbOKCancel, "Save?") = vbCancel Then Exit Sub
    
    
    Dim strDir As String
    strDir = Sheets("Todoist Checklist").Range("b48")
    strDir2 = Sheets("Todoist Checklist").Range("c48")
    strDir3 = Sheets("Todoist Checklist").Range("d48")
    
    'Makes sure there are values in boxes so folders with value 0 are not created
     Do Until False
   If Sheet11.Range("d7") = "" Then
        MsgBox "You must add a Referral Facility"
        Exit Sub
    Else
        If Sheet11.Range("d24") = "" Then
        MsgBox "You must add a LOB"
        Exit Sub
         Else
            If Sheet11.Range("D6") = "" Then
        MsgBox "You must add a Client Name"
        Exit Sub
            Else
            
            Exit Do
            End If
        End If
    End If
    Loop
    
    'Checks if folder exists.  if it doesn't it creates it.
   Do Until False
   If Dir(strDir, vbDirectory) = "" Then
        MkDir strDir
    Else
        If Dir(strDir2, vbDirectory) = "" Then
        MkDir strDir2
         Else
            If Dir(strDir3, vbDirectory) = "" Then
            MkDir strDir3
            Else
            MsgBox "Client Folder Created"
            Exit Do
            End If
        End If
    End If
    Loop
    'Saves the file into the created folder.
        ActiveWorkbook.SaveAs Sheet11.Range("C1") & "\" & Sheet11.Range("D6") & ", " & Sheet11.Range("D5") & "- PreQuote" & ".xlsm"
Sheet11.Range("a1").Value = "Saved"
End Sub



 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,949
Office Version
365
Platform
Windows
Welcome to the Board!

You probably don't have a valid path of file name.
I recommend building the file name in a variable and add error handling, i.e.
Code:
Sub TestForDirAndSave()
  If MsgBox("Are you sure?", vbOKCancel, "Save?") = vbCancel Then Exit Sub
    
    
    Dim strDir As String
    strDir = Sheets("Todoist Checklist").Range("b48")
    strDir2 = Sheets("Todoist Checklist").Range("c48")
    strDir3 = Sheets("Todoist Checklist").Range("d48")
    
    'Makes sure there are values in boxes so folders with value 0 are not created
     Do Until False
   If Sheet11.Range("d7") = "" Then
        MsgBox "You must add a Referral Facility"
        Exit Sub
    Else
        If Sheet11.Range("d24") = "" Then
        MsgBox "You must add a LOB"
        Exit Sub
         Else
            If Sheet11.Range("D6") = "" Then
        MsgBox "You must add a Client Name"
        Exit Sub
            Else
            
            Exit Do
            End If
        End If
    End If
    Loop
    
    'Checks if folder exists.  if it doesn't it creates it.
   Do Until False
   If Dir(strDir, vbDirectory) = "" Then
        MkDir strDir
    Else
        If Dir(strDir2, vbDirectory) = "" Then
        MkDir strDir2
         Else
            If Dir(strDir3, vbDirectory) = "" Then
            MkDir strDir3
            Else
            MsgBox "Client Folder Created"
            Exit Do
            End If
        End If
    End If
    Loop
    
    'Saves the file into the created folder.
[COLOR=#ff0000]     Dim fname As String[/COLOR]
[COLOR=#ff0000]     fname = Sheet11.Range("C1") & "\" & Sheet11.Range("D6") & ", " & Sheet11.Range("D5") & "- PreQuote" & ".xlsm"[/COLOR]
[COLOR=#ff0000]     On Error GoTo err_chk[/COLOR]
[COLOR=#ff0000]     ActiveWorkbook.SaveAs fname[/COLOR]
[COLOR=#ff0000]     On Error GoTo 0[/COLOR]
[COLOR=#ff0000]     Sheet11.Range("a1").Value = "Saved"[/COLOR]
[COLOR=#ff0000]     Exit Sub[/COLOR]

[COLOR=#ff0000]err_check:[/COLOR]
[COLOR=#ff0000]    MsgBox "Cannot save file to name: " & vbCrLf & fname, vbOKOnly, "ERROR!!!"[/COLOR]
    
End Sub
 
Last edited:

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
11,756
Office Version
2013
Platform
Windows
Or, Probably need the keyword Value
Code:
ActiveWorkbook.SaveAs Sheet11.Range("C1").[COLOR=#FF0000]Value[/COLOR] & "\" & Sheet11.Range("D6").[COLOR=#FF0000]Value[/COLOR] & ", " & Sheet11.Range("D5").[COLOR=#FF0000]Value[/COLOR] & "- PreQuote" & ".xlsm"
 

Jgordon

New Member
Joined
Apr 4, 2018
Messages
11
Awesome! Thanks for this!

I just ran it and it worked. I had to correct the err_check to err_chk but after that it worked.

I'll update if i run into trouble.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,949
Office Version
365
Platform
Windows
You are welcome!
I just ran it and it worked. I had to correct the err_check to err_chk but after that it worked.
Yes, typo on my part. Sorry about that.

Hopefully, the error becomes evident when it displays the calculated value.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,180
Messages
5,442,855
Members
405,203
Latest member
Mira_Xcel

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top