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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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