清除指定范围内的表单上的命名范围?

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

就像是:

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

我不知道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 

如果你的意思是删除该区域内的名字,这应该做的伎俩,请注意,它完全删除名称不只是从目标区域。 所以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