VBA saveas causing corruption

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a code opening a workbook then creating a copy under a new file name, but whenI go to open the new file, it says it is corrupted. No idea why. Any help would be greatly appreciated.

VBA Code:
Sub VEDC()
    Dim wrkMyWorkBook As Workbook
    If Dir(Sheets("Sheet1").Range("B15").Value, vbDirectory) = vbNullString Then
        MsgBox "The full path of """ & Sheets("Sheet1").Range("B15").Value & """ doesn't exist!!"
        Exit Sub
    End If
    On Error Resume Next 'Ignore any errors (i.e. if workbook is not open)
        Set wrkMyWorkBook = Workbooks(Sheets("Sheet1").Range("B15").Value)
    On Error GoTo 0 'Turn error reporting back on
    'If the 'wrkMyWorkBook' variable is Nothing then the workbook is not open, therefore we'll...
    If wrkMyWorkBook Is Nothing Then
        '...open it
        Set wrkMyWorkBook = Workbooks.Open(Filename:=Sheets("Sheet1").Range("B15").Value)
    End If
    'Application.Wait (Now + TimeValue("0:0:30"))
    
    ActiveWorkbook.SaveAs Filename:=Workbooks("Daily Reports - Copy.xlsm").Worksheets("Sheet1").Range("B16").Value, FileFormat:=xlWorkbookNormal
   'Set wrkMyWorkBook = ActiveWorkbook.SaveAs(Filename:=Sheets("Sheet1").Range("B16").Value)
    
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi
Looking at the code, it seems to be trying to open the file that is referenced in cell B15, and then saving with the filename/path at B16. Is that right? If so, perhaps you could try reverting to what I assume was the original code, but which is now commented out / in green, to see if that works? So, change the last two lines before `End Sub` to:

VBA Code:
   ' ActiveWorkbook.SaveAs Filename:=Workbooks("Daily Reports - Copy.xlsm").Worksheets("Sheet1").Range("B16").Value, FileFormat:=xlWorkbookNormal
   wrkMyWorkBook.SaveAs Filename:=Sheets("Sheet1").Range("B16").Value

If that doesn't solve it, can you tell me what error code Excel presents you with, and exactly what it says. Also, what is the value (i.e., the filepath/name) written in cell B16?
Thanks.
 
Upvote 0
Thank you for your response. That ultimately didn't work.
When I tried that code as instructed, the file does get created, but uses the cell reference in the new file to name, then gets saved to a local folder (though that workbook is functional)

When I try using the first line and commenting out the second, the file gets saved and renamed perfectly and the new file remains open. Once I close it and reopen it from the folder, it displays as "Excel cannot open the file 'daily report.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

When I try using both I get both the corrupted file and the file with the wrong name saved locally, although this time saved in a 97-03 excel format...
This is B16. though a formula behind this cell changes the path regularly.
P:\Servicing - 2. Bank Specific\VEDC - 105\5. Reporting\c. Daily Reports\2021\2021.03\03.18.2021 Daily Report.xlsx
Really not sure why I'm having issues. All I am really trying to do is take one file and make a copy in a new location with a new name based on my cell references.
 
Upvote 0
Try:

Code:
ActiveWorkbook.SaveAs Filename:=Workbooks("Daily Reports - Copy.xlsm").Worksheets("Sheet1").Range("B16").Value, FileFormat:=xlOpenXMLWorkbook
 
Upvote 0
Solution
That worked brilliantly! Out of curiosity, what's the difference between XML workbook and XL normal?
 
Upvote 0
The Normal option uses whatever is set as the default file format in your save options. It would seem that that is the old .xls format but your save name includes the .xlsx extension, hence the mismatch.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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