0

我已经搜索了几个小时的答案,并且无法再搜索了。已经看到很多关于从工作簿或工作表中删除所有范围名称的讨论。但是,我需要从单个范围中删除多个范围名称,同时将其他范围名称留在同一张工作表上和/或同一工作簿中的其他范围。代码可能看起来像这样吗?:

Sub Delete_My_Named_Ranges()
   Dim nName As Name
   Dim wbk As Workbook
   Dim Sht As Worksheet
   Dim rgNm As Range, aCell As Range

   Set wbk = Workbooks("testRgNmDelete.xlsm")
   Set Sht = wbk.Worksheets("Sheet1")

   Set rgNm = Sht.Range("$A$1")

        For Each nName In ThisWorkbook.Names
            Set aCell = Range(nName)
            If Not Intersect(aCell, rgNm) Is Nothing Then
                nName.Delete
            End If
         Next
  End Sub

好的,上面的代码适用于固定范围(“$A:$1”)。但我需要能够将 rgNm 设置为变量而不是固定范围。这是一个示例,现在错误出现在语句“Set aCell = Range(nName)”上。

Private Sub cboProductType_Change()


Dim wbSKUM As Workbook
Dim ws As Worksheet, wsLUL As Worksheet, wsLU As Worksheet
Dim rgPTL As Range, rgTable1 As Range, rgA1 As Range, rgA1LU As Range
Dim rgNm As Range, rgFormula As Range, aCell As Range
Dim sFormula As String
Dim nName As Name

Set wbSKUM = Workbooks("XBS_SKU_Master.xlsm")
Set ws = wbSKUM.Worksheets("SKUMaster")
Set wsLUL = wbSKUM.Worksheets("LookupLists")
Set wsLU = wbSKUM.Worksheets("Lookup")
Set rgPTL = wsLUL.Range("ProdTypeLookUp")
Set rgTable1 = ws.Range("Table1")
    sFormula = "=SUBSTITUTE(SUBSTITUTE(F2,"" "",""_""),""-"","""")"


 'clear Product Type Lookup List (Column D) to be sure no data remains
 wsLUL.Activate
 Range(Range("F2"), Range("F2").End(xlDown)).Select
 Selection.Cells.Value = vbNullString
    Set rgNm = Selection

    For Each nName In ThisWorkbook.Names
            Set aCell = Range(nName)
            If Not Intersect(aCell, rgNm) Is Nothing Then
                nName.Delete
            End If
    Next

再次感谢!

4

2 回答 2

1

rgName您收到该错误是因为您在声明后尚未设置。

这是我对你的问题的理解。

假设有一个范围A1:A10Sheet1单元格A2有一个名称NM1,单元格 A5 有一个名称NM2,单元格 D10 有一个名称NM3

你想要一段代码来删除 Range 中的 Names A1:A10ie NM1and NM2and notNM3

如果以上是你想要的,那么试试这个

Option Explicit

Sub Sample()
    Dim rgName As Range, aCell As Range
    Dim nName As Name

    Set rgName = Sheets("Sheet1").Range("A1:A10")

    For Each nName In ThisWorkbook.Names
        Set aCell = Range(nName)
        If Not Intersect(aCell, rgName) Is Nothing Then nName.Delete
    Next
End Sub

如果我误解了你的问题,那么你可能想改写一下?

跟进

作为一个新的 VBA 用户,这对我来说是一个非常有用的站点,但是除了这些小注释之外,我完全不知道如何添加到这个站点。请尝试想象 Excel 2010 中的名称管理器 - 在名称管理器中有 10 个唯一名称,全部用于 =Sheet1!$A$1。我希望 VBA 代码删除所有与 Sheet1!$A$1 相关的名称。我不希望在任何地方删除其他名称。– LostInData 3 分钟前

根据您的上述评论,试试这个

Option Explicit

Sub Sample()
    Dim nName As Name

    For Each nName In ThisWorkbook.Names
        If nName.RefersTo = "=Sheet1!$A$1" Then nName.Delete
    Next
End Sub
于 2013-03-28T19:00:27.993 回答
0

我认为上面给出的解决方案中缺少一些东西。

为了使用范围对象引用命名范围的范围,您必须使用命名范围的名称属性。因此,不是Set aCell = Range(nName)而是Set aCell = Range(nName.Name)

像这样:

Option Explicit

Sub Sample()
    Dim rgName As Range, aCell As Range
    Dim nName As Name

    Set rgName = Sheets("Sheet1").Range("A1:A10")

    For Each nName In ThisWorkbook.Names
        Set aCell = Range(nName.Name)
        If Not Intersect(aCell, rgName) Is Nothing Then nName.Delete
    Next
End Sub
于 2016-02-04T21:43:52.570 回答