Search code examples
vbaexceldate-rangeinputbox

Input box date range in vba?


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?


Solution

  • 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