Save current sheet as a new XLS and remove all Named Ranges

sm789

New Member
Joined
Aug 17, 2014
Messages
29
Office Version
  1. 2011
Platform
  1. MacOS
Hello,

I have a workbook with multiple sheets. I need to
Step 1: save one sheet as a new excel file;
Step 2: delete named ranges from this new saved file; and
Step 3: then delete the worksheet from the Original work book.

I have the following code which does step 1 and 3 very well.

VBA Code:
' Step 1
' Save Current Worksheet as a new File

                Application.ScreenUpdating = False
                ActiveSheet.Copy
                With ActiveWorkbook
                    .SaveAs FileName:=sFileNameXLS, FileFormat:=51
                    .Close SaveChanges:=False
                End With
                Application.ScreenUpdating = True

'Step 3
' Delete the Temp Sheet
                Application.DisplayAlerts = False
                Sheets("Temp").Delete
                Application.DisplayAlerts = True

I also have the following code for Step 2. But I am struggling to figure out where to insert this code.

I DO NOT want to remove Named Ranges from the Original Workbook, I want to remove them from the new file saved.

VBA Code:
'Step 3
' Remove all Names
 '               Dim MyName As Name
 '               For Each MyName In Names
 '               ActiveWorkbook.Names(MyName.Name).Delete
 '               Next:
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try something like this:

VBA Code:
Sub CopySheetRemoveNames()


' Step 1
' Save Current Worksheet as a new File

                Dim wbNew As Workbook, wbOrig As Workbook
                Dim shtOrig As Worksheet
                Dim sFileNameXLS As String
                
                Set wbOrig = ActiveWorkbook
                Set shtOrig = wbOrig.Worksheets("Temp")     ' <-- Change as required
                sFileNameXLS = "Test"                       ' <-- I assume you have this somewhere already

                Application.ScreenUpdating = False
                shtOrig.Copy
                
                Set wbNew = ActiveWorkbook
                
                'Step 2
' Remove all Names
                Dim MyName As Name
                For Each MyName In wbNew.Names
                 wbNew.Names(MyName.Name).Delete
                Next:
                
                With wbNew
                    .SaveAs Filename:=sFileNameXLS, FileFormat:=51
                    .Close SaveChanges:=False
                End With
                Application.ScreenUpdating = True

'Step 3
' Delete the Temp Sheet
                Application.DisplayAlerts = False
                shtOrig.Delete
                Application.DisplayAlerts = True
                
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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