0

清除工作表部分中所有命名范围的最佳方法是什么?

就像是:

Sheets("A").Range("A1:D10").ClearNames

?

4

2 回答 2

2

我不知道 ClearNames,但会这样做。样本:

Option Explicit

Sub DeleteNamedRanges()
  Dim targetWorksheet As Worksheet
  Dim targetRange As Range
  Dim nameObject As Name
  Dim namedRange As Range
  Dim unionedRange As Range

  Set targetWorksheet = Worksheets("MySheetName")
  Set targetRange = targetWorksheet.Range("A1:D10")

  For Each nameObject In ActiveWorkbook.Names
    Set namedRange = nameObject.refersToRange
    If (namedRange.Worksheet.Name <> targetWorksheet.Name) Then GoTo Continue

    Set unionedRange = Application.Union(namedRange, targetRange)
    If (unionedRange.Address = targetRange.Address) Then
      namedRange.Value = "" ' namedRange.Clear
    End If

Continue:
  Next nameObject
End Sub
于 2012-10-31T12:11:17.157 回答
0

如果您打算删除该区域内的名称,这应该可以解决问题,请注意它会完全删除名称,而不仅仅是从目标区域中删除。因此,存在于 A1:D10 内部和外部的范围也将被删除。

Option Explicit

Public Sub DeleteRangeNames()
    Dim wsTarget As Worksheet
    Dim rTarget As Range
    Dim nTmp As Name

    'Determine the range to clear all names from
    Set wsTarget = Worksheets("enternamehere")
    Set rTarget = wsTarget.Range("A1:D10")

    'Loop through all Names in the worksheet
    For Each nTmp In ActiveWorkbook.Names
        'Check if they overlap (if no overlap the intersection is Nothing)
        If Not (Intersect(nTmp.RefersToRange, rTarget) Is Nothing) Then
            'Delete the Name object from the workbook
            nTmp.Delete
        End If
    Next nTmp
End Sub
于 2012-10-31T15:18:44.170 回答