Run-time Error ActiveWorkbook.SaveAs Filename:=fpath & "\" & fname

Edgarvelez

Board Regular
Joined
Jun 6, 2019
Messages
84
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am getting the following Error:

Run-Time error '1004'
Microsoft Excel cannot access the file 'C:Users\edgar\Desktop\Working Files\N\A3221E10'.
There are several possible reasons:
The File name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a

I use the code below, in a few other workbooks and never a problem until now.
Little background, I took and existing workbook which also has this code but modified a different code and now
this code is giving me an error, I can run this same exact code on other workbooks and no problem.

VBA Code:
    Sheets("Intake Macro").Select
    Range("A1").Select
    Dim sh1 As Worksheet, sh2 As Worksheet
    Set sh1 = Sheets("Intake Macro")
    Set sh2 = Sheets("UpLoad Sheet")
    sh2.Select
    sh2.Range("A1").Select
    Set Fnd = Sheets("Upload Sheet").UsedRange.Find("Total", , , xlWhole, , , False, , False)
    If Not Fnd Is Nothing Then Fnd.EntireRow.Delete
    sh2.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).Select
    Selection.Font.Bold = True
    With Selection.Font
    .Color = -16776961
    .TintAndShade = 0
    End With
    ActiveCell.FormulaR1C1 = "X"
    ActiveCell.Offset(, 4).Resize(1, 1).Select
    ActiveCell.FormulaR1C1 = "TOTAL"
    ActiveCell.Offset(, 3).Resize(1, 4).Select
    Dim NextRow As Long
    NextRow = Range("H" & Rows.Count).End(xlUp).Row + 2
    Range("H" & NextRow & ":K" & NextRow).Formula = "=SUM(H2:H" & NextRow - 1 & ")"
    sh2.Range("A2").Select
    sh1.Select
    sh1.Range("A1").Select
    Dim fname As String
    Dim fpath As String
    Dim name As String
    Dim ws As Worksheet
    fpath = ActiveWorkbook.Path
    fname = Range("D14") & " " & Range("D15") & " " & Range("D8") & ".xlsx"
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets("UPLOAD SHEET")
    On Error GoTo 0
    If ws Is Nothing Then
    MsgBox "sheet doesn't exist"
    Exit Sub
    End If
    Dim msg
    If Dir(fpath & "\" & fname) = vbNullString Then
    ThisWorkbook.Sheets("UPLOAD SHEET").Copy
    ActiveWorkbook.SaveAs Filename:=fpath & "\" & fname
    Else
    msg = MsgBox(fname & Chr(10) & Chr(10) & "This File Name Already Exists In The Folder!!" & Chr(10) & Chr(10) & _
    "Click YES to continue to save and overwrite the file" & Chr(10) & _
    "Or NO to to cancel the Save", vbYesNo, "STOP!")
    If msg = vbYes Then
    Application.DisplayAlerts = False
    ThisWorkbook.Sheets("UPLOAD SHEET").Copy
    ActiveWorkbook.SaveAs Filename:=fpath & "\" & fname
    Application.DisplayAlerts = True
    Else
    MsgBox "File save cancelled"
    End If
    End If

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,034
Office Version
  1. 365
Platform
  1. Windows
Microsoft Excel cannot access the file 'C:Users\edgar\Desktop\Working Files\N\A3221E10'.
Is that really EXACTLY what it is returning, or did you make some typos in copying that error?

If that is really what is is returning, then I see a few things missing:

1. Looks like you are missing a "\" after the "C:". It should look like:
C:\Users\edgar\Desktop\Working Files\N\A3221E10

2. Looks like you are missing a file extension

The odd thing is, when I look at your code, it looks like those issues should already be addressed, so that is why I am questioning whether you copied the error message correctly.

Also be sure to check the following things:
1. Your file name matches exactly.
2. Your file path is correct, and you have access to it (a good check is to open up your Windows Explorer, and try browsing to the file that way, and make sure you are able to do so).
 

Edgarvelez

Board Regular
Joined
Jun 6, 2019
Messages
84
Office Version
  1. 2016
Platform
  1. Windows
Yes you are correct, (Typos).
I had added 2 additional rows to the work book and D14 should have been changed to D15 & D15 should have been changed to D16.
It is working fine now and thanks for your help.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,034
Office Version
  1. 365
Platform
  1. Windows
You are welcome.

Glad to hear everything is working as it should.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,335
Messages
5,641,555
Members
417,220
Latest member
lam150498

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top