创建新的命名范围时触发宏?

时间:2018-05-25 06:27:52

标签: excel vba excel-vba

基本上我试图在某个地方列出工作簿中的所有命名范围。但我只是希望在创建新名称或删除现有命名范围时发生这种情况。我可以用任何方式捕捉这个事件。或者仅在发生这种情况时以某种方式触发宏?

3 个答案:

答案 0 :(得分:0)

<强>更新

没有事件可以捕获命名范围的更改数量。您必须创建一些方法来检查数字。以下选项应该有效。

这需要在所有工作表代码区域(不是MODULE部分或工作簿部分)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call CheckThoseNames

End Sub

其余代码需要在模块中。顶行NamedRangeCount也需要位于模块的顶部,而不是在SubRoutines之间或底部。

Public NamedRangeCount As Integer

Sub CheckThoseNames()
If NamedRangeCount = 0 Then
    NamedRangeCount = ThisWorkbook.Names.Count

ElseIf NamedRangeCount = ThisWorkbook.Names.Count Then
    'don't do anything

Else
    Call ListTheNames

End If

End Sub

这是一旦看到命名范围数量不同就会运行的宏。我不确定你想如何列出,但希望你可以自己解决这个问题。

Private Sub ListTheNames()
Dim n As Name, InputRng As Range, i As Integer

Set InputRng = Range("A1") 'wherever you want the output to be

For Each n In ThisWorkbook.Names
    InputRng.Offset(i, 0).Value2 = n.Name
    InputRng.Offset(i, 1).Value2 = "'" & n.RefersTo
    i = i + 1
Next n

End Sub

答案 1 :(得分:0)

这可能是您最接近捕获&#34;更改命名范围事件&#34;。每次在工作表中进行选择更改时,它都会检测到命名范围更改,但如果您只是在“公式”下的“名称管理器”中进行更改,则不会检测到更改。

1) 将其插入此工作簿模块

Private Sub Workbook_Open()
    Call updateNamedRangesVar
End Sub

2)将其插入您希望检测命名范围更改的工作表模块中。 (如有必要,在所有工作表模块中插入):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call detectNamedRangeChange
End Sub

3)最后,将其插入常规模块

    Option Explicit

    Public pubNamedRanges As Variant

Sub updateNamedRangesVar()

    Dim i As Integer, namCount As Integer
    namCount = ThisWorkbook.Names.count

    If Not IsEmpty(pubNamedRanges) Then Erase pubNamedRanges

    If namCount > 0 Then
        ' take snapshot of named ranges
        ReDim pubNamedRanges(1 To namCount) As Variant
        For i = 1 To namCount
            pubNamedRanges(i) = ThisWorkbook.Names(i)
        Next i
    End If
End Sub

Sub detectNamedRangeChange()

    Dim changeBool As Boolean
    Dim i As Integer, namRngCount As Integer, namRngCountNew As Integer

    On Error Resume Next
    namRngCount = UBound(pubNamedRanges)
    On Error GoTo 0
    namRngCountNew = ThisWorkbook.Names.count

    If namRngCount <> namRngCountNew Then
        MsgBox "Change of number of named ranges from" & vbNewLine & _
            namRngCount & " to " & namRngCountNew
        changeBool = True
    Else
        If namRngCountNew > 0 Then
            With ThisWorkbook
            For i = 1 To namRngCountNew
                If CStr(.Names(i)) <> CStr(pubNamedRanges(i)) Then
                    MsgBox "Named range changed from " & vbNewLine & _
                        pubNamedRanges(i) & vbNewLine & " to " & vbNewLine & _
                        .Names(i)
                    changeBool = True
                    Exit For
                End If
            Next i
            End With
        Else
            MsgBox "Number of named ranges is now zero"
            changeBool = True
        End If
    End If

    If changeBool Then Call updateNamedRangesVar

End Sub

注意必须在sub。范围之外声明的公共变量。

答案 2 :(得分:0)

你可以使用一个功能:

Public NameCount As Long 'public 
Function CountNames(vola As Boolean) As Long
    Application.Volatile vola
    If NameCount <> ThisWorkbook.Names.Count Then
    MsgBox "start your macro here"
    NameCount = ThisWorkbook.Names.Count
    End If
    CountNames = NameCount
End Function

在工作簿的单元格中添加某个位置:= CountNames(TRUE) 确保计算设置为xlAutomatic