Runtime error 52 Bad file or number and syntax error

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
625
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
VBA Code:
FilePath = ThisWorkbook.Path & "/" & CurrMonth & "'s WO Report.xlsm"
Is there a reason you have a forward slash in the above instead of the normally expected backslash ("\")?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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