Same code works differently in a different folder ? (no fixed pathing in code)

szakharov7723

Board Regular
Joined
Jun 22, 2018
Messages
85
Office Version
  1. 2019
Platform
  1. Windows
So my code works differently in a the same file, but in a different file path


Sub SaveAsExample()


Code:
Dim FName           As String
Dim FPath           As String
On Error GoTo Handler:
    FPath = Sheets("Setup").Range("A1").Text
    FName = Sheets("Setup").Range("G1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
    Exit Sub
Handler:
    FPath = Sheets("Setup").Range("A1").Text
    FName = Sheets("Setup").Range("G1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName & "_2"


End Sub

The way it should work:
First it saves the file based on date in cell and filepath in cell
IF the file already exists ,it naturally asks if I want to replace this file , if I say yes --it replaces. If I say no --it creates file name and adds _2.

BUT when all I do is paste this file to another folder ,it no longer asks me if I want to replace the file ,and adds _2 right away, if file name already exists.
Any ideas how is it possible ?
 
Sorry for creating confusion, I don't know myself how I removed it when I copied the code, but it is not the case, as I the code itself doesn't have this issue
I will show what debug tells me
Ssf8kNz
bugsplat.png
[/URL][/IMG]
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
What does the error message say?

I do not have problems with SaveCopyAs, but in the code you have SaveAs, you could try it with SaveCopyAs

only for tests, you could also change the date format for something shorter.
 
Upvote 0
Error message: Run-time error '1004':
Cannot access 'Friday, February 01, 2019.xlsb'.

I think for some reason it cannot replace the same file. As you see in screenshot I have February 01, 2019 open , so when I try to Save As (thus replacing this file) it cannot do so.[When I tried to open a Template file and save as February 01, 2019 with the same macro, it allowed me to do so.] Maybe I need to use replace, if file name already exists and I confirm to replace it.

As for SaveCopyAs
I tried this code on empty workbook. It does work, but for some reason it doesn't work in workbook I need.
 
Upvote 0
You should carefully check the code, try with savecopyas, but also check the backslash "" and also check that the backup copy is in another folder, because if you try to save it in the same folder it will send you the error.
 
Upvote 0
I managed to get this working (I hope)

Code:
Sub savefilecopy()
    Dim FName   As String
    Dim FPath   As String
    Dim wAns    As Variant
    Dim FExt    As String
    
    FPath = Sheets("Setup").Range("A1").Value
    FName = Format(Sheets("Setup").Range("G1").Value, "dddd, mmmm dd, yyyy")
    FExt = ".xlsb"
    
    If Dir(FPath, vbDirectory) = "" Then
        MsgBox "The folder not exists", vbCritical
        Exit Sub
    End If
        
    If Dir(FPath & "\" & FName & FExt) <> "" Then
        wAns = MsgBox("The file already exists. Do You want to replace it", vbQuestion & vbYesNoCancel, "Save Copy")
        Select Case wAns
            Case vbYes
                Application.DisplayAlerts = False
                ThisWorkbook.SaveCopyAs Filename:=FPath & FName & FExt
                Application.DisplayAlerts = True
            Case vbNo
                Application.DisplayAlerts = False
                ThisWorkbook.SaveCopyAs Filename:=FPath & FName & "_2" & FExt
                Application.DisplayAlerts = True
            Case vbCancel
                MsgBox "Process canceled"
        End Select
    Else
        ThisWorkbook.SaveCopyAs Filename:=FPath & FName & FExt
    End If
End Sub
The reason why I eliminated the "" because it has already been included in cell formula, so it ended up double dash like \\Friday. I think it didn't work in previous code because I didn't remove "" from code.

Thank you a lot it seems like working now.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
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