VBA Copy worksheets into seperate workbooks - paste values

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
182
I hope someone can help me with a bit of VBA I need to customise.

I want to copy worksheets into individual workbooks using their tab name.
When copying them over, I need to paste the contents of the worksheets as values only.

I thought I could change the code to ".PasteSpecial Paste:=xlValues"
but this doesn't work for me, it leaves a blank worksheet.

Code:
Sub SaveShtsAsBook()
      Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
      MyFilePath$ = ActiveWorkbook.Path & "\" & _
                    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
      With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            '      End With
            On Error Resume Next    '<< a folder exists
            MkDir MyFilePath            '<< create a folder
            For N = 1 To Sheets.Count
                  Sheets(N).Activate
                  SheetName = ActiveSheet.Name
                  Cells.Copy
                  Workbooks.Add (xlWBATWorksheet)
                  With ActiveWorkbook
                        With .ActiveSheet
                              .Paste
                              .Name = SheetName
                              [A1].Select
                        End With
                        'save book in this folder
                        .SaveAs Filename:=MyFilePath _
                                          & "\" & SheetName & ".xls"
                        .Close SaveChanges:=True
                  End With
                  .CutCopyMode = False
            Next
      End With
      Sheet1.Activate
End Sub


It would be a big bonus if I could also specify which sheets to export instead of exporting everything.
ie "Example sheet 1" , "Example sheet 2" & "Example sheet 3"
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
182
I just discovered a new problem.
I want to retain the defined name ranges on the worksheet but when the worksheets are copied, they lose that data.

For each worksheet I want to copy, "A5" is defined as "Family"

Can this be integrated in the vba?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,506
Messages
5,837,745
Members
430,515
Latest member
K_Lynch

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
Top