Is it possible to create a dynamic drop down list based on the current year? For example, I want to create a dropdown based on the current year (2019); which have 5 options:
Then, starting 1/1/2020, the dropdown would automatically update to 2020, 2021, 2022, 2023, and 2024. Is this possible to do with some expression in the data validation drop down?
"Dynamic" validation in VBA could be achieved through rewriting the validation every time, when it is needed. In the worse case, it could be every time the worksheet is opened or even every time when a selection is changed, probably through events.
The validation in excel is could be list and in VBA this list is passed as a string separated with commas. Thus, the string 2019, 2020, 2021, 2022, 2023
is quite a good example of the expected list. There are different ways to do it, probably with a loop would be the "fanciest one", but as far as the values are only 5, then writing them manually is probably suitable as well.
TL DR - run this in a Workbook_Open()
event:
Sub TestMe()
Dim validationString As String
validationString = Year(Now()) & ", " _
& Year(Now()) + 1 & ", " _
& Year(Now()) + 2 & ", " _
& Year(Now()) + 3 & ", " _
& Year(Now()) + 4
With Worksheets(1).Cells(1, "A").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:=validationString
End With
End Sub
Place these formulas in two separate tables without headers & starting in row 1
Year:
=YEAR(TODAY())+ROW()-1
Month column 1:
=SWITCH([@Column2],1,"Jan",2,"Feb",3,"Mar",4,"Apr",5,"May",6,"Jun",7,"Jul",8,"Aug",9,"Sep",10,"Oct",11,"Nov",12,"Dec")
and column 2:
=MONTH(DATE(YEAR(TODAY()),MONTH(TODAY())+ROW()-1,DAY(TODAY())))
..and you'll get dynamic references that start from current date
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.