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
 
Yeah it has me stumped.. Would it be easier to just add a sequential document number at the end of the name if a file already exists with a the specified name? Like Microsoft does for files.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Yes, you could do something like 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"
            Do While Dir(spath) <> ""
               Dim n As Long
               n = n + 1
               spath = .SelectedItems(1) & Application.PathSeparator & Sheets("Sheet1").Range("L26") & "_" & n & ".xlsm"
            Loop
            ActiveWorkbook.SaveAs Filename:=spath, FileFormat:=52
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Holy cow. Thank you so much! I've been losing my mind just trying to get ANYTHING to work.. But this worked! Thank you!
 
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