问题
I have an excel file and in the first column (A) i have some dates like this:
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?
回答1:
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
回答2:
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.
In toolbox right click and select additiona controls
Then in the list box select Microsoft Date and Time Picker Control:
Add the control to your form:
then set the code as follows under the DTPicker1_Change() Event:
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.
来源:https://stackoverflow.com/questions/19430765/input-box-date-range-in-vba