为数据验证列表动态生成年份数组 Excel VBA
Dynamically generate array of Years for data validation list Excel VBA
我正在向 table 中的单元格添加数据验证下拉列表,并希望用从 Today()+1
到 10 years back
的年份数组动态填充它。例如
2020,2019,2018,2017,2016,2015,2014,2013,2012,2011,2010
。我尝试将 shorthand 用于 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
- 你的数学有误:
[Transpose(Text(date( (year(today())+2) - row(1:10), 1, 1), "yyyy"))]
- 当使用
xlValidateList
时,它需要一个逗号分隔的列表而不是数组。所以创建和数组变量并使用 Join:
Dim arr As Variant
arr = [Transpose(Text(date( (year(today())+2) - row(1:10), 1, 1), "yyyy"))]
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:=Join(arr, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Cells(1).Value2 = "<Year>"
End With
为什么不用简单的循环而不是用公式填充数组:
Dim arr(1 To 10) As Long
Dim i As Long
For i = 1 To 10
arr(i) = Year(Date) + 2 - i
Next i
使用方括号表达式定义一个工作簿范围的命名范围:
现在您的数据验证公式可以简单地为 =Years
。
根据经验,避免在 VBA 代码中使用方括号表达式。它适用于直接窗格中的一次性代码和调试器指令,不适用于生产代码。
我正在向 table 中的单元格添加数据验证下拉列表,并希望用从 Today()+1
到 10 years back
的年份数组动态填充它。例如
2020,2019,2018,2017,2016,2015,2014,2013,2012,2011,2010
。我尝试将 shorthand 用于 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
- 你的数学有误:
[Transpose(Text(date( (year(today())+2) - row(1:10), 1, 1), "yyyy"))]
- 当使用
xlValidateList
时,它需要一个逗号分隔的列表而不是数组。所以创建和数组变量并使用 Join:
Dim arr As Variant
arr = [Transpose(Text(date( (year(today())+2) - row(1:10), 1, 1), "yyyy"))]
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:=Join(arr, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Cells(1).Value2 = "<Year>"
End With
为什么不用简单的循环而不是用公式填充数组:
Dim arr(1 To 10) As Long
Dim i As Long
For i = 1 To 10
arr(i) = Year(Date) + 2 - i
Next i
使用方括号表达式定义一个工作簿范围的命名范围:
现在您的数据验证公式可以简单地为 =Years
。
根据经验,避免在 VBA 代码中使用方括号表达式。它适用于直接窗格中的一次性代码和调试器指令,不适用于生产代码。