Delete Named Ranges That Referring to Another Workbook

BigBeachBananas

Active Member
Joined
Jul 13, 2021
Messages
450
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
For context, I currently have a VBA that copies a sheet from sourceWB to destWB. My issue is that when you copy a sheet, the named ranges get copied over to the destWB as well. I'm looking for a VBA code that deletes any named ranges that refer to the sourceWB. I'm thinking of looking for any named ranges under RefersTo that start with a single quote and deleting it, however, my code doesn't look like it's doing it.

VBA Code:
Sub DeleteNamedRangesReferringToOtherWorkbooks()
    Dim wb As Workbook
    Dim nm As Name
    Dim deleteList As New Collection
    Dim nameToDelete As String

    ' Loop through all open workbooks
    For Each wb In Workbooks
        ' Loop through all named ranges in the current workbook
        For Each nm In wb.Names
            nameToDelete = nm.Name
            ' Check if the named range refers to another workbook
            If Left(nameToDelete, 1) = "'" Then
                ' Add the named range to the delete list
                deleteList.Add nameToDelete
            End If
        Next nm
    Next wb

    ' Delete the named ranges in the delete list
    For Each nameToDelete In deleteList
        On Error Resume Next ' Ignore errors if the named range doesn't exist
        ThisWorkbook.Names(nameToDelete).Delete
        On Error GoTo 0 ' Reset error handling
    Next nameToDelete
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Assuming the following code is in the destination workbook, try this:
VBA Code:
Sub Lose_Copied_Named_Ranges()
    Dim xName As Name
    For Each xName In Application.ThisWorkbook.Names
        If InStr(1, xName.RefersTo, "'") > 0 Then xName.Delete
    Next xName
End Sub
 
Upvote 1
Solution
Hi BBB. Trial transferring your sheet using a collection. It will fix the named range issue. HTH. Dave
Code:
Dim ShtCollect As Collection
Set ShtCollect = New Collection
ShtCollect.Add sourceWB.Sheets("Sheet1")
ShtCollect(1).Copy before:=destWB.Sheets("Sheet1")
Adjust the sheet name(s) to suit.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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