Exporting tabs in a range as values

ardykav

Board Regular
Joined
Oct 18, 2015
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a file with a lot of tabs and have had a macro (below) thats always worked fine. It basically exports specific tabs into a new workbook with all the data in it moved across as values.

However now I want it to export a number of tabs but rather then specifying the tabs in the code I just want it to pick up a named range called "Tabsforexport" on the list tab.

I am guessing I just need to edit the code below where it specifies the array but not entirely sure of the best approach.

thanks in advance


VBA Code:
Sub Exportas()
    Dim NewName As String
    Dim nm As Name
    Dim ws As Worksheet
    
    If MsgBox("This will copy sheets to a new workbook" _
    , vbYesNo, "Product Exporter") = vbNo Then Exit Sub
    
    With Application
        .ScreenUpdating = False
        
         '       Copy specific sheets
        On Error GoTo ErrCatcher
        Sheets(Array("CSV", "MQV")).Copy
        On Error GoTo 0
        
         '       Paste sheets as values
         '       Remove External Links, Hperlinks and hard-code formulas
         '       Make sure A1 is selected on all sheets
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            ws.Cells.Hyperlinks.Delete
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
        
         '       Remove named ranges
        For Each nm In ActiveWorkbook.Names
          
        Next nm
        
         '       Input box to name new file
        NewName = InputBox("Please Specify the name of your new workbook", "What do you want to call your new workbook?")
        
         '       Save it with the NewName and in the same directory as original
        ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & NewName & ".xls"
        'ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & NewName & ".pdf"
        ActiveWorkbook.Close SaveChanges:=False
        
        .ScreenUpdating = True
        MsgBox "File Exported"
    End With
    Exit Sub
    
ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this.
VBA Code:
Sub Exportas()
Dim ws As Worksheet
Dim nm As Name
Dim NewName As String
Dim arrSheets As Variant

    If MsgBox("This will copy sheets to a new workbook" _
              , vbYesNo, "Product Exporter") = vbNo Then Exit Sub

    arrSheets = Range("Tabsforexport").Value ' Sheets("List").Range("Tabsforexport").Value
    
    arrSheets = Application.Transpose(arrSheets)
    
    With Application
        .ScreenUpdating = False

        '       Copy specific sheets
        On Error GoTo ErrCatcher
        Sheets(arrSheets).Copy
        On Error GoTo 0

        '       Paste sheets as values
        '       Remove External Links, Hperlinks and hard-code formulas
        '       Make sure A1 is selected on all sheets
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            ws.Cells.Hyperlinks.Delete
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select

        '       Remove named ranges
        For Each nm In ActiveWorkbook.Names

        Next nm

        '       Input box to name new file
        NewName = InputBox("Please Specify the name of your new workbook", "What do you want to call your new workbook?")

        '       Save it with the NewName and in the same directory as original
        ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & NewName & ".xls"
        'ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & NewName & ".pdf"
        ActiveWorkbook.Close SaveChanges:=False

        .ScreenUpdating = True
        
        MsgBox "File Exported"
        
    End With
    
    Exit Sub

ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub
 
Upvote 0
I have a bit of an issue with this one, the file and tabs export but for some reason it doesnt paste the formulas that were in the original file across as values, they dont actually come across at all.

Also is there a way that I can specify only Columns A-P to be copied from all the tabs in the array?

VBA Code:
Sub createmsl()
Dim ws As Worksheet
Dim nm As Name
Dim NewName As String
Dim arrSheets As Variant

    If MsgBox("This will copy sheets to a new workbook" _
              , vbYesNo, "Product Exporter") = vbNo Then Exit Sub

    arrSheets = Range("MLS").Value ' Sheets("List").Range("Tabsforexport").Value
    
    arrSheets = Application.Transpose(arrSheets)
    
    With Application
        .ScreenUpdating = False

        '       Copy specific sheets
        On Error GoTo ErrCatcher
    
          Sheets(arrSheets).Copy
        On Error GoTo 0

        '       Paste sheets as values
        '       Remove External Links, Hperlinks and hard-code formulas
        '       Make sure A1 is selected on all sheets
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            ws.[A1].PasteSpecial Paste:=xlValues
            ws.Cells.Hyperlinks.Delete
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select

        '       Remove named ranges
        For Each nm In ActiveWorkbook.Names

        Next nm

l
        ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & "MSL File.xls"
    
        ActiveWorkbook.Close SaveChanges:=False

        .ScreenUpdating = True
        
        MsgBox "File Exported"
        
    End With
    
    Exit Sub

ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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