Copy Variable Named Worksheets to Single Files

grady121

Active Member
Joined
May 27, 2005
Messages
385
Office Version
  1. 2016
Platform
  1. Windows
As a Newbie I have inherited a file that, with the exeption of two named worksheets "Entry" & "Data" that I don't need, exports only specific named worksheet into a new workbook, with the new filename based on the existing worksheet name.

What I'm trying to acheive is for the code to export all worksheets (Not "Entry" & "Data") into a separate file as before, but regardless of sheetname. I am trying to add worksheets with variable named sheets so can't hard code.

Is it possible for someone to modify my existing code to achieve this.

Present code:
Sub SaveWorksheet_as_Workbook(SheetName As String, SavePath As String, SaveName As String)
Dim NewWorkbookName As String
Dim MasterWorkbook As String
Dim TempString As String
Dim P1Name As String

Application.ScreenUpdating = False
WorkbookName = ActiveWorkbook.Name
MasterWorkbook = WorkbookName

If SheetName = "Entry" Then
Exit Sub
ElseIf SheetName = "Data" Then
Exit Sub

ElseIf SheetName = "Monday" Then

Application.ScreenUpdating = False
P1Name = SaveName & "_" & "Monday"
WorkbookName = ActiveWorkbook.Name
Workbooks.Add xlWorksheet
Sheets("Sheet1").Name = P1Name
ActiveWindow.DisplayGridlines = False
Call SetPrint

ActiveWorkbook.SaveAs FileName:=SavePath & "\" & P1Name & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.ScreenUpdating = False
Workbooks.Open FileName:=SavePath & "\" & P1Name & ".xls", UpdateLinks:=0
Windows(WorkbookName).Activate
Sheets("Monday").Select
Cells.Select
Selection.Copy
Windows(P1Name & ".xls").Activate
Cells.Select
ActiveSheet.Paste
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.BreakLink Name:=SavePath & "\" & P1Name & ".xls", Type:= _
xlExcelLinks
ActiveWorkbook.Close
Range("C2").Select

ElseIf SheetName = "Tuesday" Then
etc

End Sub

Any help appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Here's a shot from the hip. It's tough to troubleshoot something this involved from afar.

In the procedure that calls "SaveWorksheet_as_Workbook" you could put something like the following. This would filter out the sheets you don't care about without cluttering your main proceedure with "If Thens".

Rich (BB code):
Dim oSheet As Worksheet

For Each oSheet In ThisWorkbook
 
    Debug.Print oSheet.Name
 
    Select Case oSheet.Name
        Case "Entry"
            'Do nothing
        Case "Data"
            'Do nothing
        Case Else
            'Execute main proceedure for everything other than the two above named sheets.
             SaveWorksheet_as_Workbook oSheet.Name, SavePath, SaveName

    End Select

Next oSheet
The code you posted was already getting a "SheetName" from whatever it is that calls it.

Sub SaveWorksheet_as_Workbook(SheetName As String, SavePath As String, SaveName As String)

I'm not sure I spotted all the references to the target worksheet name in your code, but you will want to change all the hard coded references ("Monday" etc) to the variable "SheetName" (w/o quotes).

Here's the code you posted with some stuff commented out and a few comments added.

Rich (BB code):
Dim NewWorkbookName As String
Dim MasterWorkbook As String
Dim TempString As String
Dim P1Name As String
 
Application.ScreenUpdating = False
WorkbookName = ActiveWorkbook.Name
MasterWorkbook = WorkbookName
 
'If SheetName = "Entry" Then
'Exit Sub
'ElseIf SheetName = "Data" Then
'Exit Sub
'ElseIf SheetName = "Monday" Then
 
Application.ScreenUpdating = False

'P1Name = SaveName & "_" & "Monday"
P1Name = SaveName & "_" & SheetName
 
WorkbookName = ActiveWorkbook.Name
Workbooks.Add xlWorksheet
 
Sheets("Sheet1").Name = P1Name ' Shouldn't this be just "SheetName" and not the entire "SaveName" ???? There is a limit on length of sheet names.
 
ActiveWindow.DisplayGridlines = False
Call SetPrint
ActiveWorkbook.SaveAs Filename:=SavePath & "\" & P1Name & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.ScreenUpdating = False
Workbooks.Open Filename:=SavePath & "\" & P1Name & ".xls", UpdateLinks:=0
Windows(WorkbookName).Activate
 
'Sheets("Monday").Select
Sheets(SheetName).Select ' Name of the sheet passed into the 
proceedure ???
 
Cells.Select
Selection.Copy
Windows(P1Name & ".xls").Activate
Cells.Select
ActiveSheet.Paste
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.BreakLink Name:=SavePath & "\" & P1Name & ".xls", Type:= _
xlExcelLinks
ActiveWorkbook.Close
Range("C2").Select
 
'ElseIf SheetName = "Tuesday" Then
'etc
 
End Sub

I hope this helps.

Gary
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

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