VBA - Copy only a few sheets from an .xlsm file (whit macros) to a new .xlsx file (whitout macros)

Rogerant79

New Member
Joined
Oct 14, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Is there a way to copy some sheets from an excel file to a new file to create?
I have an .xlsm file with VBA macros.
I would like to save only some sheets of this file in another file that does not have any vba macros (so a simple file with .xlsx extension).
In the source file (.xlsm) I have several sheets, 4 of which are visible and 6 others are hidden (very hidden).
So I would like to copy only the 4 visible sheets of the source file, in a new file with the extension .xlsx.
Also I would like the new file created during the whole process not to be opened.
Can anyone help me out?
Thanks
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
I think the code below does what you ask, although I don't quite understand your next comment:

Also I would like the new file created during the whole process not to be opened.

VBA Code:
Public Sub ClonePartialWorkbook()

    Dim oWb As Workbook, oWs As Worksheet, sThisFile As String, sNewFile As String
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        Set oWb = ThisWorkbook
        sThisFile = oWb.FullName
        sNewFile = Left(oWb.FullName, InStrRev(oWb.FullName, ".")) & "xlsx"
        For Each oWs In oWb.Worksheets
            If oWs.Visible = xlSheetHidden Or oWs.Visible = xlSheetVeryHidden Then
                oWs.Visible = xlSheetVisible
                oWs.Delete
            End If
        Next oWs
        oWb.SaveAs Filename:=sNewFile, FileFormat:=xlOpenXMLWorkbook
        .Workbooks.Open sThisFile
        .DisplayAlerts = True
        .ScreenUpdating = True
        oWb.Close SaveChanges:=False
    End With
End Sub
 

Maurice123

New Member
Joined
Jan 6, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi, I try the above VBA code but get error massage in the line:

Set oWb = ThisWorkbook


The error code I have:
1614449508447.png


What should I do?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,060
Messages
5,622,461
Members
415,895
Latest member
Akhilesh28

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