Hello
I have an Excel spreadsheet saved on my One Drive for Business called "MasterXXX.xlsm" which I am trying to run the VBA script below, however I am getting the run time error "Error 52 Bad File Name".
When I try it in my own local drive, I can run it perfectly.
I have already looked at different forum/pages, and I did not find any solution to solve this issue.
Could anyone help me, pls?
Upload Image
I have an Excel spreadsheet saved on my One Drive for Business called "MasterXXX.xlsm" which I am trying to run the VBA script below, however I am getting the run time error "Error 52 Bad File Name".
When I try it in my own local drive, I can run it perfectly.
I have already looked at different forum/pages, and I did not find any solution to solve this issue.
Could anyone help me, pls?
VBA Code:
Sub consWB()
Dim wb As Workbook, sh As Worksheet, lr As Long, lc As Long, fName As String, fPath As String
fPath = ThisWorkbook.Path 'Code is run from workbook in same directory
Debug.Print fPath
If Right(fPath, 1) <> "\" Then fPath = fPath & "\" 'Be sure separator is in path string
fName = Dir(fPath & "*.xl*", vbNormal) '******Error 52 Bad File Name******* or Number 'Any Excel file
Debug.Print fName
Do While fName <> ""
If fName <> ThisWorkbook.Name Then 'Avoid trying to open a second instance the workbook running the code
Set wb = Workbooks.Open(fPath & fName)
For Each sh In wb.Sheets 'This For Next loop can be replaced with other code to do other things.
lr = sh.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
lc = sh.Cells.Find("*", , xlFormulas, xlPart, xlByColumns, xlPrevious).Column
sh.Range("A2", sh.Cells(lr, lc)).Copy ThisWorkbook.Sheets(sh.Name).Cells(Rows.Count, 1).End(xlUp)(2)
Next
End If
ThisWorkbook.Save 'This is optional, but can be useful in case of catastrophic failure of the system.
wb.Close False 'Avoids alert messages if there are auto calculations in the opened workbook, like date updates, etc.
fName = Dir
Loop
End Sub