Runtime error 52 Bad file or number and syntax error

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
This code runs inside a template workbook. When the workbook opens it will check and see if the following workbook name exists, if not, it will create a saved workbook copy with that name, otherwise it will open that workbook. I am not sure if this matters, but this file is saved to OneDrive. The error 52 message occurs on this line:
VBA Code:
If Len(Dir(FilePath)) = 0 Then
and the syntax error occurs here: The following part of the code is highlighted in blue "C:\Users\"
VBA Code:
Set wb1 = ActiveWorkbook.SaveCopyAs "C:\Users\" & Environ("username") & "\OneDrive - CompanyName\Desktop\" & CurrMonth & "'s WO Report.xlsm"

VBA Code:
    Dim wb1 As Workbook
    Dim FilePath As String
 
    CurrMonth = Format(DateAdd("M", 0, Now), "MMMM")
 
    FilePath = ThisWorkbook.Path & "/" & CurrMonth & "'s WO Report.xlsm"
    If Len(Dir(FilePath)) = 0 Then
        Set wb1 = ActiveWorkbook.SaveCopyAs "C:\Users\" & Environ("username") & "\OneDrive - CompanyName\Desktop\" & CurrMonth & "'s WO Report.xlsm"
    Else
        Set wb1 = Workbooks.Open(FilePath)
    End If
   
    Set CheckForExistingWorkbooks = wb1
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,224,813
Messages
6,181,109
Members
453,021
Latest member
Justyna P

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