引用名称包含在单元格中的命名范围

时间:2014-11-11 16:45:16

标签: excel range formula

我在工作表中包含200多个命名范围,每个名称都包含在A列的列表中。我可以通过A列中的名称引用命名范围吗?

例如,一个命名范围是指E10:E16,并命名为' BlueCount' (包含在单元格A2中)。如果我输入以下任何公式,我会得到正确的结果:

=SUM(BlueCount)
=SUM(E10:E16)

但是,如果我尝试=SUM(INDIRECT(A2)),则会出现#REF!错误。同样,如果我以任何其他方式(例如=SUM("Blue"&"Count"))分解公式中的名称,则会出现同样的问题。

有没有办法通过引用另一个单元格中存储为字符串的名称来引用命名范围,或者只能在编写公式时直接引用命名范围?

非常感谢您的帮助!

更新:命名范围是动态命名范围,并使用以下代码创建:

Sub CreateNames()

    Dim WB As Workbook, WS As Worksheet
    Dim FAVLRow As Long, FAVLCol As Long, i As Long
    Dim MyName As String, Start As String

    Const RowNo = 7
    Const Offset = 1
    Const ColNo = 3
    Set WB = ActiveWorkbook
    Set WS = ActiveSheet

    FAVLCol = WS.Cells(RowNo, 5).End(xlToRight).Column
    FAVLRow = WS.Cells(Rows.Count, ColNo).End(xlUp).Row
    Start = Cells(RowNo, ColNo).Address

    WB.Names.Add Name:="FAVLCol", _
             RefersTo:="=COUNTA($" & RowNo & ":$" & RowNo & ")"
    WB.Names.Add Name:="FAVLRow", _
             RefersTo:="=COUNTA($C$9:$C$100000)"
    WB.Names.Add Name:="MyData", RefersTo:= _
              "=" & Start & ":INDEX($1:$65536," & "FAVLRow," & "FAVLCol)"

    For i = ColNo To FAVLCol
        MyName = Replace(Cells(RowNo, i).Value, " ", "_")

        WB.Names.Add Name:=MyName, RefersToR1C1:= _
         "=OFFSET(R" & RowNo & "C" & i & ",2,0,COUNTA(R9C3:R100000C3),1)"
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

检查你的拼写。打开一个新的空白工作表并运行以下命令:

Sub BlueCountTest()
    Dim r As Range
    Set r = Range("E10:E16")
    With r
        .Name = "BlueCount"
        .Value = 1
    End With
    Range("A2") = "BlueCount"
    Range("A3").Formula = "=SUM(INDIRECT(A2))"
End Sub

A3 应显示 7