Excel 2010 VBA Formula1引用带有选项卡名称变量的命名范围*或*单元格位置

时间:2013-01-24 22:00:41

标签: excel vba range

在VBA(Excel 2010)中,我

  1. 动态创建命名范围列表
  2. 使用该列表在另一列中创建下拉选项
  3. 创建下拉列表时,(a)使用命名范围似乎不起作用,(b)如果我不使用命名范围 - 并且需要按工作表名称引用单元格引用,我遇到了麻烦,因为我的工作表刚刚今天更名为。

    这很麻烦,我知道,但这是我到目前为止所做的:

    ' find the name of the worksheet and replace it with today's date
    Dim vTabOriginalName As String
    Dim vTabDateName As String
    
    Dim vRangeName As String
    
    vRangeName = "StageListChoices"
    
    vTabOriginalName = ActiveSheet.Name
    vTabDateName = Format(Now(), "yyyy-mmm-dd")
    ActiveSheet.Name = vTabDateName
    
    'create a drop down list for the stage (col K)
    
    Range("AK3").Value = "NO ACTIVITY"
    Range("AK4").Value = "SOLICITATION"
    Range("AK5").Value = "OPPORTUNITY"
    
    ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=(vTabDateName & "!R3C37:R5C37")
    
    
    '~~> Creates the list
    With Range("K2:K" & vReportRowCount).Validation  'report row count known earlier
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=StageListChoices"  
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    

    创建指定区域时录制的宏足够有意义了:

    ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:= _
        "=2013-JAN-24!R3C37:R14C37"
    ActiveWorkbook.Names("StageListChoices").Comment = ""
    

    最初,我一直在使用String变量在VBA中创建下拉列表,但是" real" list是15项,我在重新打开文件时遇到错误,验证时间过长(?)因此已经关闭。

    基本上,我尝试过这样的事情:

    Formula1:="=StageListChoices"
    
    Formula1:=vRangeName
    
    Formula1:="=vRangeName"
    
    Formula1:=vTabDateName & "!R3C37:R5C37"
    

    我抬头看到的一切都说第一个(Formula1:=" = StageListChoices")应该有效 - 但它没有。

    谢谢!

1 个答案:

答案 0 :(得分:2)

更改

ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=(vTabDateName & "!R3C37:R5C37")

ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=("='" & vTabDateName & "'!R3C37:R5C37")

您错过了='