SaveAs 'Cannot access file' problem

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
The code below stops with the error "Runtime error 1004: Cannot access THURSDAY 10.5.12.xls" at the red line. The workbook of that name already exists in that folder, and should prompt me whether I want to save over it or not (as a similar macro I have used for years did) but stops with this error. Anyone know why, please? Thanks for your time.
Nigel

Code:
Sub GetSOInfo()
    Dim so As Worksheet, f As Worksheet, ri As Range, cel As Range, col As Range, day As Integer, DateInc As Integer
    Dim c As Integer, p As Integer, a, OldDate As String, NewDate As String
    Dim dte As Date, dc As String, dp As String
    dte = Worksheets("Workpad").[b10]
    If WorksheetFunction.Weekday(dte) = 7 Then
        DateInc = 2
    Else
        DateInc = 1
    End If
    OldDate = UCase(WorksheetFunction.Text(dte, "DDDD")) & " " & WorksheetFunction.Text(dte, "D.M.Y")
    NewDate = UCase(WorksheetFunction.Text(dte + DateInc, "DDDD")) & " " & WorksheetFunction.Text(dte + DateInc, "D.M.Y")
    If MsgBox("Do you want to set up the standing order figures for " & NewDate & "?" & vbCr & vbCr & _
        "THIS WILL CLEAR ALL EXISTING FIGURES FROM THE ORDER SHEET", vbYesNo, "ARE YOU SURE?") = vbNo Then Exit Sub
    Application.ScreenUpdating = False
    [COLOR="Red"]ActiveWorkbook.SaveAs Filename:="\\Think\c\NICE BUNS\NICE BUNS BAKERY\ORDERS\DAILY ORDERS\" & OldDate & ".xls"[/COLOR]
    MsgBox OldDate & " saved sucessfully"

.....etc
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The only think I can think of is that the file is in use (?). I generally take a "paranoid" approach and delete the file explicitly/check that it was deleted:
Code:
DIM FSO as Object
Dim sFile As String
sFile = "\\Think\c\NICE BUNS\NICE BUNS BAKERY\ORDERS\DAILY ORDERS\" & OldDate & ".xls"

Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(sFile) Then
    FSO.DeleteFile sFile
End If
If FSO.FileExists(sFile) Then
    MsgBox "Error: file " & sFile " cannot be deleted."
    '//Appropriately abort the code here
End If

Causes of undeletable files are sometimes obscure though. I have no problem 99.5% of the time and once in a blue moon for no apparent reason I can't delete a file.
 
Upvote 0
You can explicitly reference the FileSystemObject too:

  1. In the visual basic editor go to Tools | References
  2. Check the box for "Microsoft Scripting Runtime"

Now in your code you can declare the object:
Code:
[COLOR="Red"]Dim FSO As FileSystemObject
[/COLOR]Set FSO = CreateObject("Scripting.FileSystemObject")

This has the advantage of giving you intellisense when you use the object. The FSO object is a little more sophisticated than inbuilt VBA file commands, and I have a suspicion it understands UNC filepaths a little better, will read/write unicode, and so on, so it's slightly more flexible in practice.
 
Upvote 0
If you are using a Ctrl-Shift key combo to run your macro and you get this error 1004, try running it instead from the the VBA editor and pushing F5. If the error goes away, try removing the "Shift" key from your macro key combination.

I've had this 1004 run-time error intermittantly with macros launched with a "shift" key combo when opening workbooks from the macro. It is a bug in Microsoft that affects workbooks opening, and perhaps with your code saving a workbook.

It was a baffling intermittant error I could never figure out until I ran across this article:

http://www.jkp-ads.com/Articles/WorkbookOpenBug.asp
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,316
Members
448,564
Latest member
ED38

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
Back
Top