VBA in 2010 copied sheet to new workbook fine; in 2016 the current workbook closes before the code is complete

MacroPolo

New Member
Joined
Dec 19, 2019
Messages
2
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
My company is upgrading computers and Office from 2010 to 2016. A workbook had VBA code (created via macro, so probably not as optimized as possible) to update data in a form on a sheet from a table, save the values in the form, and then copy that sheet to a new workbook using the data to save it with a unique filename. When it finished saving the new workbook, the code looped to the next record in the source workbook and repeated those steps until it reached the end of the list. Now that we have switched to Excel 2016, the code runs fine until it tries to copy the sheet with the form to a new workbook. It opens the new workbook and closes the source one which interrupts the code execution. As far as I can tell there were no other changes except moving from Win7 and Excel 2010 to Win10 and Excel 2016. Any thoughts why the source workbook would now suddenly be closing?

VBA Code:
Sheets("Form").Copy
    ChDir strNewFilePath
    ActiveWorkbook.SaveAs Filename:=strNewFilePath & strNewFileName, FileFormat _
    :=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.WindowState = xlNormal
    With ActiveWindow
        .Top = 49
        .Left = 85
    End With
    Windows("OriginalForm.xlsm").Activate
    With ActiveWindow
        .Top = -0.5
        .Left = 7.75
    End With
    Workbooks(strNewFileName).Close
    ActiveWindow.WindowState = xlMaximized
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try it with this time delay added.

Code:
Windows("OriginalForm.xlsm").Activate
    With ActiveWindow
        .Top = -0.5
        .Left = 7.75
    End With
Dim wt As Integer
wt = Timer + 2
    Do While Timer < wt
        DoEvents
    Loop
    Workbooks(strNewFileName).Close
    ActiveWindow.WindowState = xlMaximized
This gives a two second delay. If you need more just change the 2 to a higher number. If you use decimals then you have to Dim wt As Double.
 
Upvote 0
Try it with this time delay added.
...
This gives a two second delay. If you need more just change the 2 to a higher number. If you use decimals then you have to Dim wt As Double.
Thanks for the reply. I added the timer loop, but when I stepped through in debug mode the problem is encountered on the first line of the code I posted.
VBA Code:
Sheets("Form").Copy
Reading the MS document on that method indicates it is supposed to copy the sheet to a new workbook. After multiple trials, I've found it opens a new workbook, but does not even copy the sheet. On a couple of occasions, it reopened the original file in recovery mode.
 
Upvote 0
You are having a problem!
Code:
Sheets("Form").Copy
should create a new workbook with the original sheet as sheet 1, and the only sheet in that new workbook. This is the first that I have heard where it created a workbook with an empty sheet.
I don't know what to tell you about the behavior you describe, except it is not normal.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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