?
Solved

How to have Message Box ask date range in excel

Posted on 2011-04-26
11
Medium Priority
?
211 Views
Last Modified: 2012-08-13
What's the proper way to write a macro to have a MSG Box pop up and ask a date range (for example) or other search criteria from a column in a spreadsheet?   (and then can I have those rows pasted on another sheet?)    Thanks
0
Comment
Question by:SRMoxy
  • 6
  • 5
11 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35466747
Use Application.InputBox rather than MsgBox. If you need a range, then you will probably want to use two InputBox statements (or a userform):
dim startrow as long
startrow = Application.inputbox(Prompt:="Enter a start row number", type:=1)

Open in new window

for example
0
 

Author Comment

by:SRMoxy
ID: 35467498
So if I use Application.InputBox for "End Date"    -  how do I designate the date column so it will give me rows that match that criteria?      (ie:  all rows with due dates that are before "End Date")
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35467549
Use an autofilter. This would be a lot easier with a sample workbook and a clear example of what you are trying to achieve. :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:SRMoxy
ID: 35467765
Here's a brief example, hope it makes sense.   ( If an area is planning downtime in September, for instance,  I'd like them to be able to hit a command button, then put the date in the InputBox and end up with a list of items due by that date.)     Sample1.xls     So the criteria I'm looking for is in column E.     Thanks for the help.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35467935
Try this:
Sub FilterData()
   Dim dteEnd As Date
   Dim wksData As Worksheet, wksOutput As Worksheet
   
   Set wksData = Sheets("Sheet1")
   Set wksOutput = Sheets.Add
   
   On Error Resume Next
   dteEnd = CDate(Application.InputBox(prompt:="Enter final due data to filter by", Title:="Last date", Type:=2))
   On Error GoTo 0
   
   If dteEnd <> #12:00:00 AM# Then
      With wksData.Range("A2").CurrentRegion
         .AutoFilter field:=5, Criteria1:="<=" & CLng(dteEnd)
         If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
            .Copy wksOutput.Range("A1")
         Else
            MsgBox "No data for " & dteEnd
         End If
         .AutoFilter
      End With
   End If
      
End Sub

Open in new window

0
 

Author Comment

by:SRMoxy
ID: 35468205
That works awesome!     Can I add an InputBox to filter for a single department (within this same macro) ?    PS:  do I need to submit this as a new question???   Thanks so much for your help!
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35468224
That would be easy enough to add in but you don't have a department field that I can see?
0
 

Author Comment

by:SRMoxy
ID: 35468296
Sorry ~  here's a modified version "Sample2"   - I added the department column and deleted
one so the due date column is in the same position.   Thanks Sample2.xls
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 35468386
Here you go. Note that both of these need you to delete the blank row below the headers to be reliable:
Sub FilterData()
   Dim dteEnd As Date
   Dim wksData As Worksheet, wksOutput As Worksheet
   Dim strDept As String
   Set wksData = Sheets("Sheet1")
   Set wksOutput = Sheets.Add
   
   On Error Resume Next
   dteEnd = CDate(Application.InputBox(prompt:="Enter final due data to filter by", Title:="Last date", Type:=2))
   On Error GoTo 0
   strDept = Application.InputBox(prompt:="Enter Department to filter by", Title:="Choose dept", Type:=2)
   If dteEnd <> #12:00:00 AM# Then
      With wksData.Range("A2").CurrentRegion
         .AutoFilter field:=5, Criteria1:="<=" & CLng(dteEnd)
         .AutoFilter field:=1, Criteria1:=strDept
         If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
            .Copy wksOutput.Range("A1")
         Else
            MsgBox "No data for " & dteEnd
         End If
         .AutoFilter
      End With
   End If
      
End Sub

Open in new window

0
 

Author Comment

by:SRMoxy
ID: 35468528
Thanks So Much!!    Have a good day!
0
 

Author Closing Comment

by:SRMoxy
ID: 35468540
Quick response was great!  Thanks ~
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question