【问题标题】:Convert named range scope from Worksheet to Workbook by overwriting only works for some named ranges通过覆盖将命名范围范围从工作表转换为工作簿仅适用于某些命名范围
【发布时间】:2020-05-26 22:02:28
【问题描述】:

我已经编写了代码(来自几个来源,底部的引用),这些代码将以编程方式获取工作表范围的命名范围并将它们转换为工作簿命名范围。但是我的代码只适用于某些命名范围,而不适用于其他范围,我不知道为什么。

我必须这样做的原因是因为我必须从原始来源中删除两个选项卡(一个包含 _T 和另一个 _X 的选项卡)并从另一个来源复制这些选项卡的副本。这给我留下了#REF!#REF 的工作簿范围命名范围和具有我想要的范围但我需要它们工作簿范围的工作表命名范围。

见下方代码

如果我运行此代码以寻找“_T”,它会完美运行。所有以 _T 开头的名为 #REF!#REF 的工作簿命名范围现在都具有正确的范围,并且它们对应的名为范围的工作表将被删除。但是,如果我运行此程序以查找“_X”,则名为 range 的工作簿将保持不变。我难住了。我什至尝试了一种不同的方法,即手动删除所有以_X 开头的名为范围的当前工作簿,然后以编程方式尝试使用ActiveWorkbook.Names.Add Name:=newNm,RefersTo:=nm.RefersTo 添加它们,这也无济于事(甚至不添加新记录)。

提前感谢您的帮助。

Sub WStoWBscope()

Dim nm As Name, Ans As Integer, newNm As String, fltr As String

fltr = "_X" 'search string


For Each nm In ActiveWorkbook.Names                'look at all named ranges within the current workbook
    If nm.Name Like "X!*" Then                     'looks for worksheet scoped named range that has the correct range
        If InStr(1, nm.Name, fltr) > 0 Then
            newNm = Replace(nm.Name, "X!", "")     'save name of existing workbook named range
            Range(nm.RefersTo).Name = newNm        'overwrite workbook named range with proper range
            nm.Delete                              'deletes worksheet named range
        End If
    End If
Next nm
End Sub

VBA to Convert Named Ranges Workbook to Worksheet Scope VBA to change the scope of named ranges from worksheet level to workbook

【问题讨论】:

  • If nm.Name Like "X!*" 仅匹配以“X”开头的名称,因此您不会在这些名称中找到“_X”(至少不在名称的开头)
  • 明白,但这实际上不是问题。我说的是我正在寻找以_X 开头的命名范围。但是,nm.Names 下的整个条目是 Sheet+[命名范围]。代码的InStr() 部分正在搜索命名范围,然后在变量newNm 中,我从全名中删除了工作表。正如我所说,代码在我正在寻找 _T 的情况下可以正常工作(我也在用 T 替换 X!)
  • 可能对调试有用。打印一些问题名称的示例并将它们包含在您的帖子中。

标签: excel vba named-ranges


【解决方案1】:

试试这个:

Sub ConvertWorksheetNamedRangesToWorkbookNamedRanges()
    Dim nName As Name
    'Loop Through each named Range
    For Each nName In ActiveWorkbook.Names
        'Is Name scoped at the Workbook level?
        If TypeOf nName.Parent Is Workbook Then

        End If
        'Is Name scoped at the Worksheet level?
        If TypeOf nName.Parent Is Worksheet Then
            ' If nm.Name Like "X!*" Then .....
            ' Do the filtering you need
            ' ....
                Dim sName As String
                sName = nName.Name 'Save the name of the name
                Dim rngName As Range
                Set rngName = Range(nName) ' Save the range of the name
                nName.delete    ' Delete the name
                'Create a new one on workbook scope
                ThisWorkbook.Names.Add Name:=sName, RefersToR1C1:="=" & rngName.Address(ReferenceStyle:=xlR1C1)
            ' End If
        End If
    Next nName
End Sub

【讨论】:

  • 维克托,谢谢。这成功了。这似乎是语义,但这使它正常工作,所以我不能抱怨。我仍然使用newNm,因为我有它而不是 sName,但是范围、删除、添加我合并的段,并且它起作用了。再次感谢。
  • 仅供参考,我赞成您的解决方案,但我的代表太低,无法注册
【解决方案2】:

仅出于文档目的(不要赞成这个,赞成 Viktor 的回答),代码的最终版本如下所示:

Dim nm As Name, Ans As Integer, newNm As String, fltr As String, rngName As Range

'Filter named ranges that contain specific phrase
fltr = "_X"

'Search for all names in the workbook
For Each nm In ActiveWorkbook.Names
    'Search within those named ranges by those that have a specific worksheet scope
    If nm.Name Like "X!*" Then
        'Search for the named ranges of a type set by your filter (fltr)
        If InStr(1, nm.Name, fltr) > 0 Then
            'Take the full name [Scope]+[named range] and remove the scope
            newNm = Replace(nm.Name, "X!", "")
            'save the original range used by the worksheet-scoped named range 
            Set rngName = Range(nm)
            'delete the worksheet-scoped named range
            nm.Delete    
            'Create/Overwrite a workbook-scoped named range (this does overwrite any workbook-scoped named ranges that are the same name with #REF!#REF )
            ThisWorkbook.Names.Add Name:=newNm, RefersToR1C1:="=" & rngName.Address(ReferenceStyle:=xlR1C1)
        End If
    End If
Next nm
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多