vba vba中的输入框日期范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19430765/
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
Input box date range in vba?
提问by David_D
I have an excel file and in the first column (A) i have some dates like this:
我有一个 excel 文件,在第一列 (A) 中,我有一些这样的日期:
17/10/2013
18/10/2013
19/10/2013
20/10/2013
21/10/2013
22/10/2013
The other columns contains some datas. I need create an input box that takes everything inside a date range. I mean; i click the button and it shows me a popup like:
其他列包含一些数据。我需要创建一个输入框,将日期范围内的所有内容都包含在内。我的意思是; 我单击按钮,它显示了一个弹出窗口,如:
insert start date: 17/10/2013 (i can decide the date)
insert end date: 20/10/2013 (i can decide the date)
and then i can put a macro i've done. So my macro read only datas inside that range. Is it possible?
然后我可以放一个我已经完成的宏。所以我的宏只读取该范围内的数据。是否可以?
回答by user2140261
You can Add a date Time Picker to your use form and test the input for your range as follows:
您可以将日期时间选择器添加到您的使用表单并测试您的范围的输入,如下所示:
Open the VBA and the form you want the input on.
打开 VBA 和您想要输入的表单。
In toolbox right click and select additiona controls
在工具箱中右键单击并选择附加控件
Then in the list box select Microsoft Date and Time Picker Control:
然后在列表框中选择 Microsoft 日期和时间选择器控件:
Add the control to your form:
将控件添加到您的表单中:
then set the code as follows under the DTPicker1_Change() Event:
然后在DTPicker1_Change()事件下设置代码如下:
Private Sub DTPicker1_Change()
If DTPicker1.Value < DateSerial(2013, 10, 20) And DTPicker1.Value > DateSerial(2013, 10, 15) Then
Debug.Print "Put Your Code Here"
End If
End Sub
change the dates to your own and add your code.
将日期更改为您自己的日期并添加您的代码。
Notes:There are a large amount of settings inside this control, from colors to display types, checkboxes, dropdown style, default values minimum and maximum dates. And also custom Date Formats can be applies based on locale.
注意:此控件中有大量设置,从颜色到显示类型、复选框、下拉样式、默认值最小和最大日期。还可以根据语言环境应用自定义日期格式。
回答by Portland Runner
There is no specific date type so you'll have to do a bit of error checking. Here is one idea of how to get a date from a user:
没有特定的日期类型,因此您必须进行一些错误检查。以下是如何从用户那里获取日期的一个想法:
Dim dateString As String, TheDate As Date
Dim valid As Boolean: valid = True
Do
dateString = Application.InputBox("Enter A Date: ")
If IsDate(dateString) Then
TheDate = DateValue(dateString)
valid = True
Else
MsgBox "Invalid date"
valid = False
End If
Loop Until valid = True