fselliott
asked on
VBA code to copy Excel Worksheets to single Worksheet, and then save to new Excel file.
I need to copy Excel Worksheets to a single Worksheet, and then save the new Worksheet as it's own file - without the VBA code in place. I have written the following code, but it has two errors that I can easily see. First, it loops - the Excel file never allows closure. Second, the single Worksheet file contains no data. The file is attached, and the code is as follows:
Private Sub Workbook_BeforeClose(Cance l As Boolean)
Dim ws As Worksheet
Dim r As Long
Application.ScreenUpdating = False
'Delete contents of Master sheet - row 2 downwards (clear from the previous Save action)
Worksheets("MASTER").Activ ate
With ActiveSheet.UsedRange
.Offset(1, 0).Resize(.Rows.Count - 1).Select
End With
'Loop through all Worksheets
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> ActiveSheet.Name And ws.Name <> "Data" Then
'Copy starting at 2nd Row
ws.UsedRange.Offset(2, 0).Copy
'Paste data to Master Worksheet, below last used row
Cells(ActiveSheet.UsedRang e.Rows.Cou nt + 1, 1).PasteSpecial xlPasteValues
End If
Next ws
'Remove any blank rows
For r = ActiveSheet.UsedRange.Rows .Count To 2 Step -1
If Application.WorksheetFunct ion.CountA (Rows(r)) = 0 Then
Rows(r).Delete
End If
Next r
Application.DisplayAlerts = False
'Write Worksheet to external file - do not include any VBA Code or Macros
Range("A1").Select
ActiveWorkbook.Save
Sheets("Master").Select
Range("A1:R1245").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir "Y:\Projects"
ActiveWorkbook.SaveAs Filename:= _
"Y:\Projects\data.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
ProjectSchedule.xlsm
Private Sub Workbook_BeforeClose(Cance
Dim ws As Worksheet
Dim r As Long
Application.ScreenUpdating
'Delete contents of Master sheet - row 2 downwards (clear from the previous Save action)
Worksheets("MASTER").Activ
With ActiveSheet.UsedRange
.Offset(1, 0).Resize(.Rows.Count - 1).Select
End With
'Loop through all Worksheets
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> ActiveSheet.Name And ws.Name <> "Data" Then
'Copy starting at 2nd Row
ws.UsedRange.Offset(2, 0).Copy
'Paste data to Master Worksheet, below last used row
Cells(ActiveSheet.UsedRang
End If
Next ws
'Remove any blank rows
For r = ActiveSheet.UsedRange.Rows
If Application.WorksheetFunct
Rows(r).Delete
End If
Next r
Application.DisplayAlerts = False
'Write Worksheet to external file - do not include any VBA Code or Macros
Range("A1").Select
ActiveWorkbook.Save
Sheets("Master").Select
Range("A1:R1245").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir "Y:\Projects"
ActiveWorkbook.SaveAs Filename:= _
"Y:\Projects\data.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False
, CreateBackup:=False
Application.CutCopyMode = False
Application.ScreenUpdating
Application.DisplayAlerts = True
End Sub
ProjectSchedule.xlsm
ASKER
So my code now receives an error on MASTER.Paste, and this is what is looks like (BTW, I am very unfamiliar with VBA so I can only hope I followed your instructions well):
Private Sub Workbook_BeforeClose(Cance l As Boolean)
Dim ws As Worksheet
Dim r As Long
Dim MASTER As Worksheet
Set MASTER = ThisWorkbook.Sheets("MASTE R")
Application.ScreenUpdating = False
'Delete contents of Master sheet - row 2 downwards (clear from the previous Save action)
Worksheets("MASTER").Activ ate
With MASTER.UsedRange
.Offset(1, 0).Resize(.Rows.Count - 1).Select
End With
'Loop through all Worksheets
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> MASTER.Name And ws.Name <> "Data" Then
'Copy starting at 2nd Row
ws.UsedRange.Offset(2, 0).Copy
'Paste data to Master Worksheet, below last used row
Cells(MASTER.UsedRange.Row s.Count + 1, 1).PasteSpecial xlPasteValues
End If
Next ws
'Remove any blank rows
For r = MASTER.UsedRange.Rows.Coun t To 2 Step -1
If Application.WorksheetFunct ion.CountA (Rows(r)) = 0 Then
Rows(r).Delete
End If
Next r
Application.DisplayAlerts = False
'Write Worksheet to external file - do not include any VBA Code or Macros
Range("A1").Select
ActiveWorkbook.Save
MASTER.Copy
Workbooks.Add
MASTER.Paste
Application.CutCopyMode = False
ChDir "Y:\Projects"
ActiveWorkbook.SaveAs Filename:= _
"Y:\Projects\data.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_BeforeClose(Cance
Dim ws As Worksheet
Dim r As Long
Dim MASTER As Worksheet
Set MASTER = ThisWorkbook.Sheets("MASTE
Application.ScreenUpdating
'Delete contents of Master sheet - row 2 downwards (clear from the previous Save action)
Worksheets("MASTER").Activ
With MASTER.UsedRange
.Offset(1, 0).Resize(.Rows.Count - 1).Select
End With
'Loop through all Worksheets
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> MASTER.Name And ws.Name <> "Data" Then
'Copy starting at 2nd Row
ws.UsedRange.Offset(2, 0).Copy
'Paste data to Master Worksheet, below last used row
Cells(MASTER.UsedRange.Row
End If
Next ws
'Remove any blank rows
For r = MASTER.UsedRange.Rows.Coun
If Application.WorksheetFunct
Rows(r).Delete
End If
Next r
Application.DisplayAlerts = False
'Write Worksheet to external file - do not include any VBA Code or Macros
Range("A1").Select
ActiveWorkbook.Save
MASTER.Copy
Workbooks.Add
MASTER.Paste
Application.CutCopyMode = False
ChDir "Y:\Projects"
ActiveWorkbook.SaveAs Filename:= _
"Y:\Projects\data.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False
, CreateBackup:=False
Application.CutCopyMode = False
Application.ScreenUpdating
Application.DisplayAlerts = True
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would suggest changing:
Open in new window
to:Open in new window
Then use Master. in place of Activesheet as this is far less volatile
to copy the MASTER sheet to a new workbook... just use Master.copy
this will automatically create a new active workbook with just Master as a sheet.
then can name activeworkbook and save it.