首页 文章

名为range的VBA检索为sheetname和cell地址

提问于
浏览
0

我有一个例程,可以为估算和实际工作表添加新的客户范围以及验证,公式和保护 . 我将命名范围添加到某些单元格中,因为它们在公式中的整个工作表中使用 .

命名范围的代码部分如下(它起作用,范围在名称管理器中正确命名)

执行名称范围的函数声明为

Function InitializeAddedRange(rangeToFormat As Range, _
    custName As String, currentSheet As Worksheet)

并且如下调用

Call InitializeAddedRange(startCell, customerName, sourceWorksheet)

所以在下面的片段中,rangeToFormat是startCell

Set rangeToFormat = rangeToFormat.Offset(0, 1)
If currentSheet.Name = "Actual Quarterly Sales" Then
    rangeToFormat.Name = Replace(custName, " ", "") & "ActualBasePrice"
    rangeToFormat.Offset(0, 1).Name = Replace(custName, " ", "") & "ActualIsCurrent"
ElseIf currentSheet.Name = "Quarterly Sales Projections" Then
    rangeToFormat.Name = Replace(custName, " ", "") & "BasePrice"
    rangeToFormat.Offset(0, 1).Name = Replace(custName, " ", "") & "IsCurrent"
End If

在一个在同一子程序中调用但在上面的代码之后调用的函数中,我试图在许多公式中检索该范围的名称 . (startCell.Name)

Set tempRange = Range(startCell.Offset(1, 3), startCell.Offset(1, 14))
For Each cCell In tempRange
    With cCell
        .Value = 0
        .NumberFormat = "$#,##0"
        .FormulaR1C1 = "=DrumGallons*" & startCell.Name & "*R[-1]C"
        .Locked = True
    End With
Next cCell

问题是 startCell.Name 返回工作表名称和单元格地址,即 SalesEstimates$B$12 而不是应该是 customernameBasePrice 的命名范围 . 我记得曾经遇到过这个问题 . 是否发生这种情况是因为在函数尝试访问它之前,子程序还没有在excel中更改?我没有尝试过无数可行的事情 .

2 回答

  • 0

    无法测试它所以我只是想回忆一下 . 尝试使用

    Name(startCell)
    

    获取附加到Range参考的名称

  • 0

    我认为你需要解决名称对象

    rng.Name.Name
    

相关问题