vba ComboBox 中的今天日期和未来日期下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22977592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
todays date and future date dropdown list in a ComboBox
提问by Hush
every time i start up the file and click the macro for the userform, the combobox should have the list of dates from today and future dates, it can not have past dates.
每次我启动文件并单击用户表单的宏时,组合框都应该包含今天和未来日期的日期列表,它不能包含过去的日期。
this is the code that I have, it only inputs the date in the combobox and formats the value of the list. but i don't know how to list down the future dates in it.
这是我拥有的代码,它只在组合框中输入日期并格式化列表的值。但我不知道如何在其中列出未来的日期。
Public Sub UserForm_initialize()
ComboBox3.Value = Format(Date, "dd/mm/yyyy")
ComboBox3.Value = Format(ComboBox3.Value, "dd/mm/yyyy")
End Sub
回答by David Zemens
Instead of Date
use Now()
. "Date" is a semi-reserved keyword for a data type. You may be able to get away with using it as a variable, but it would be ambiguous/confusing to do so.
而不是Date
使用Now()
. “日期”是数据类型的半保留关键字。您可能可以将其用作变量,但这样做会模棱两可/令人困惑。
The way I would do this is to use the listbox .AddItem
method in a loop, with some date arithmetic, using the DateAdd
function (super handy, if you work with dates a lot!).
我这样做的.AddItem
方法是在循环中使用 listbox方法,使用一些日期算法,使用DateAdd
函数(超级方便,如果你经常使用日期!)。
Public Sub UserForm_initialize()
Dim i as Integer
Dim myDate as Date
myDate = Now()
For i = 0 to 10 'Add the next 10 days, for example
ComboBox3.AddItem Format(DateAdd("d", i, myDate), "dd/mm/yyyy")
Next
ComboBox3.ListIndex = 0
End Sub