在 VBA 公式中定义和使用名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18424563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:29:09  来源:igfitidea点击:

Defining and using names in VBA formulas

excelvbaexcel-vba

提问by Hyman Knudson

Alright, I'm pretty new to VBA and I ran into this roadblock.

好吧,我对 VBA 还很陌生,我遇到了这个障碍。

I want to define a name for a column of dates ("dates") in VBA, and then use that name in a VBA formula.

我想在 VBA 中为一列日期(“日期”)定义一个名称,然后在 VBA 公式中使用该名称。

Here's where I define the name in excel:

这是我在excel中定义名称的地方:

Sub Build_dates(as_of_date As String, curve_source As String)
'Code
Range("B14").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="dates", RefersTo:=Selection.Address
'More Code
End Sub

And here's the sub I wish to use it in:

这是我希望使用它的子程序:

Sub Build_times(interp_count As String, as_of_date As String)
    Dim First As Range, Last As Range, fillRange As Range
    Set First = Range("C14")
    Set Last = Range("B14").End(xlDown).Offset(0, 1)
    Set fillRange = Range(First.Address & ":" & Last.Address)
    Select Case interp_count
        Case "Act/360"
            First.Formula = "=(dates-$B)/360"
            First.AutoFill Destination:=fillRange
        Case "Act/365"
            First.Formula = "=(dates-$B)/365"
            First.AutoFill Destination:=fillRange
    End Select
    'Add name to the time column
    Range("C14").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Names.Add Name:="times", RefersTo:=Selection.Address
End Sub

The problem is that every cell in the "times" column has #VALUE! in it. I looked in the "Show Calculation Steps" option in the contextual menu that appears over each of the cells and saw this:

问题是“时间”列中的每个单元格都有#VALUE!在里面。我查看了出现在每个单元格上方的上下文菜单中的“显示计算步骤”选项,并看到了这一点:

("$B:$B"-36526)/360

Which evaluates the parenthesized value to #VALUE!

它将括号内的值计算为#VALUE!

Neither of the names that I define appear in the drop-down names menu in the upper-left, but I can find both dates and times in the names manager, with the each referring to

我定义的名称都没有出现在左上角的下拉名称菜单中,但我可以在名称管理器中找到日期和时间,每个都指的是

="$B:$B"

and

="$A:$A"

respectively. Whereas one that I define by manually highlighting a range and typing the name in the name box yields a reference with something like

分别。而我通过手动突出显示范围并在名称框中键入名称来定义的一个引用会产生类似的引用

='Bootstrap Validation'!$H:$H

How should I change my code so that I can use the named ranges in VBA defined formulas?

我应该如何更改我的代码,以便我可以在 VBA 定义的公式中使用命名范围?

回答by chris neilsen

To answer the direct question, pass a Range(not a String) to RefersTo, like this

要回答直接问题,请将 a Range(不是 a String)传递给RefersTo,就像这样

ActiveWorkbook.Names.Add Name:="dates", RefersTo:=Selection

That said, you should not use Select/Selectionfor code like this. See this answerfor some examles of how to avoid these.

也就是说,您不应该将Select/Selection用于这样的代码。有关如何避免这些问题的一些示例,请参阅此答案