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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,864
Members
449,052
Latest member
Fuddy_Duddy

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