Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88126

Moving sheet from one WB to another New WB

$
0
0

I have made the following code for moving sheet from one WB to another New WB.

However I am experiencing errors.

Sub MoveSheets01()
   Dim ws As Worksheet
   Dim newWB As Workbook
   Dim oldwb As Workbook

   Application.ScreenUpdating = False
   Set oldwb = ActiveWorkbook
   Set newWB = Application.Workbooks.Add

   oldwb.Activate

   For Each ws In oldwb.Sheets
      If ws.Name <> "Input" And ws.Name <> "Output" Then
         Application.DisplayAlerts = False
         ws.Copy after:=newWB.Sheets(newWB.Sheets.Count)
         ws.Delete
         Application.DisplayAlerts = True
      End If
   Next ws

   oldwb.Save
   newWB.Activate
   Application.DisplayAlerts = False
   Sheets("Sheet1").Delete
   Application.DisplayAlerts = True
   newWB.SaveAs Filename:=oldwb.Path & "\AAA "& Format(Now(), "DD.MMM.YYYY hh.mm AMPM") & ".xlsx", CreateBackup:=False
End Sub

It generates a new WB.

But the moment I save either, the file crashes.


Viewing all articles
Browse latest Browse all 88126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>