【发布时间】:2020-02-28 02:04:39
【问题描述】:
我正在向表格中的单元格添加数据验证下拉列表,并希望使用从 Today()+1 到 10 years back 的年份数组动态填充它。例如
2020,2019,2018,2017,2016,2015,2014,2013,2012,2011,2010。我尝试使用Evaluate 的简写,即[],但由于某些原因,我收到了错误 2015。
With .ListColumns("Select Year").DataBodyRange
With .Cells(1).Validation
.Delete
'.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="<Year>,2020, 2019,2018,2017,2016,2015,2014,2013,2012,2011,2010"
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=[Transpose(Text(date( (year(today())+1) + row(1:10), 1, 1), "yyyy"))]
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Cells(1).Value2 = "<Year>"
End With
公式为:
Formula1:=[Transpose(Text(date( (year(today())+1) + row(1:10), 1, 1), "yyyy"))]
为什么突然不工作了?是否有另一种快速生成年份的方法?
这是我在下面添加的屏幕截图。在我运行以下代码之前,它一直在工作:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Name = "Instructions" Then
ThisWorkbook.Sheets("Instructions").Visible = True
Target.Follow ' here is where error occurred and evaluate stopped working!
End If
End Sub
【问题讨论】:
-
方括号表达式写在哪里(在哪个模块中)?如果它在工作表的代码隐藏中,我怀疑它在工作表的上下文中工作(即
Worksheet.Evaluate) - 否则,它在ActiveSheet的上下文中工作(即Application.Evaluate)。您可能希望通过将方括号表达式替换为显式传递给适当的Evaluate方法的字符串来明确上下文。 -
我正在
Module中测试它。尝试了Application.Evaluate并传递了一个字符串,但仍然得到Error 2015。见这里:arr = Application.Evaluate("Transpose(Text(date( (year(today())+2) - row(1:10), 1, 1), ""yyyy""))")
标签: excel vba date validation evaluate