Solved

Find Row Between Two Dates Excel Wookbook

Posted on 2014-02-19
6
408 Views
Last Modified: 2014-02-19
I have an Excel Workbook with 8 tabs. I would like to find all the rows (records) between two dates and put the resulst on a seperate tab (entire row).
0
Comment
Question by:shieldsco
  • 4
  • 2
6 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 39870758
could you upload a sample ?
gowflow
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39870762
Were are your dates lying ? in what column ? all in the same Column ?
gowflow
0
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 39870941
Well having not answered my questions, I made up a sample workbook with Dates in Col A and here is the code for that

Sub GetData()
Dim WS As Worksheet
Dim WSResult As Worksheet
Dim sDate As String, eDate As String
Dim MaxRow As Long

If MsgBox("This process will lookup all sheets in this workbook for a date range in Col A and will return in this sheet all the rows where the data falls in between the selected dates." & Chr(10) & Chr(10) _
    & "Are you ready to proceed ?", vbQuestion + vbYesNo, "Get Data") = vbYes Then
    
    Set WSResult = Sheets("Results")
    WSResult.Cells.Delete
    
    '---> Start Date
    Do
        sDate = InputBox("Input starting Date: ", "Start Date", Format(Now, "mm/dd/yyyy"))
    Loop Until IsDate(sDate)
    
    '---> End Date
    Do
        eDate = InputBox("Input ending Date: ", "End Date", Format(Now, "mm/dd/yyyy"))
    Loop Until IsDate(eDate)
    
    For Each WS In ActiveWorkbook.Worksheets
        If WS.Name <> "Results" Then
        
            '---> Filter the data as per dates
            WS.UsedRange.AutoFilter field:=1, Criteria1:=">=" & sDate, Operator:=xlAnd, Criteria2:="<=" & eDate
            If WS.Rows.End(xlDown).Row < 1048576 Then
                '---> first time put the header
                If MaxRow = 0 Then
                    WS.Range("1:1").Copy WSResult.Range("A1")
                    MaxRow = 2
                End If
                
                WS.Range("A2", "A" & WS.Rows.End(xlDown).Row).EntireRow.SpecialCells(xlCellTypeVisible).Copy
                WSResult.Range("A" & MaxRow).PasteSpecial
                MaxRow = WSResult.UsedRange.Rows.Count + 1
            End If
            WS.ShowAllData
            
        End If
    Next WS
    
    '---> Advise user
    WSResult.UsedRange.EntireColumn.AutoFit
    If MaxRow - 2 = 0 Then
        MsgBox ("No rows were found in the date range " & sDate & " - " & eDate)
    Else
        MsgBox ("A total of " & MaxRow - 2 & " rows were found in the date range " & sDate & " - " & eDate & " and copied successfully.")
    End If
End If

End Sub

Open in new window


the attached sample workbook is for you to see how it works.
Activate macros and press on the button in sheet Results. Put dates interval when required as start and end date. It verify that dates are indeed dates and not strings or non dates. then it goes thru all the sheets and give the results in sheet Results.

Let me know
gowflow
FindDateRange.xlsm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shieldsco
ID: 39870976
I was at lunch - yes the dates are all in the same column -- I will test your code
0
 

Author Closing Comment

by:shieldsco
ID: 39871152
Works Great - Thanks
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39871335
your welcome.
gowflow
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now