Not good enough with VBA to understand this error.

Brassaxe

New Member
Joined
Aug 6, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
This code allows you to simply click a button, open a dialog to define the save location and hit OK. Then it pulls the text from a defined cell and saves the file.

This all works fine unless there is a File will the same name in the same location. Then it gives runtime error "1004", and cannot access "file name".

I would like it to either overwrite the file or Add a sequential number to the end of the file name and then save. Is this possible?


VBA Code:
Sub SaveFile()
    Application.ScreenUpdating = False
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            sPath = .SelectedItems(1)
            ActiveWorkbook.SaveAs Filename:=sPath & Application.PathSeparator & Sheets("Sheet1").Range("L26") & ".xlsm", FileFormat:=52
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:

VBA Code:
Sub SaveFile()
    Application.ScreenUpdating = False
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            sPath = .SelectedItems(1) & Application.PathSeparator & Sheets("Sheet1").Range("L26") & ".xlsm"
            If Dir(sPath) <> "" then Kill sPath
            ActiveWorkbook.SaveAs Filename:=sPath , FileFormat:=52
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hmmm. You're getting me further along. Now it's giving me runtime error 70, permission denied on the Kill sPath.
 
Upvote 0
That would suggest that you can't delete or overwrite the specified file for some reason (e.g. incorrect permissions, or it's open somewhere).
 
Upvote 0
Yes. I want it to be able to save itself. So do I need it to first check if the same file exists, and if so, save, and it not, save as (specified name)?
 
Upvote 0
Is the currently specified name (that is causing the permissions error) the same as the current name of the file that is running the code? That would explain the error.
 
Upvote 0
Yes. Is there a way around the error. IE, simply save and not save as?
 
Upvote 0
Yep - try this:

VBA Code:
Sub SaveFile()
    Application.ScreenUpdating = False
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            sPath = .SelectedItems(1) & Application.PathSeparator & Sheets("Sheet1").Range("L26") & ".xlsm"
            If lcase$(activeworkbook.fullname) = LCase$(spath) then
                activeworkbook.save
            else
                If Dir(sPath) <> "" then Kill sPath
                ActiveWorkbook.SaveAs Filename:=sPath , FileFormat:=52
             end if
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Same error as above. When saving the open file, and a file of the same name exists is doesn't just save it gives runtime error 70, permission denied on the Kill sPath.

Is the only way to get around this to kill the open file? Why can it not save as a normal file would save?
 
Upvote 0
It's not killing the open file (unless you also happen to have another workbook open that has the name of the file you are trying to save to). It should be killing the existing file on disk, but it sounds like you don't have the correct permissions to do that for some reason. I can't replicate your error.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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