+ Reply to Thread
Results 1 to 18 of 18

Date range look up and copy/paste coding

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Date range look up and copy/paste coding

    Hi,

    I have a little problem with some coding that I am working on.
    I need some code I can assign to a button that, once clicked will prompt the user for a date range (dd/mm/yyyy to dd/mm/yyyy).
    The code will then use this date range to search through upwards of 200 worksheets for any matches.
    If found, it will copy certain data from those sheets, to a weekly summary sheet.

    I have the code that gets the date range in the form of variables, but I don't know enough to write the code that will search the sheets and copy the required data.

    Here is an example spreadsheet.

    Any help on this would be greatly appreciated.

    Ed

    date range look up and copy.xlsx
    Last edited by mungel; 02-27-2012 at 02:36 PM. Reason: adding prefix

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Date range look up and copy/paste coding

    mungel,

    Attached is a modified version of the example workbook you posted. The button has been assigned to the following macro:
    Sub tgr()
        
        Dim iCalc As Integer
        Dim strTemp As String
        Dim StartDate As Date
        Dim EndDate As Date
        Dim ws As Worksheet
        Dim rngName As Range
        Dim rngID As Range
        Dim rngDate As Range
        Dim rngVis As Range
        Dim VisCell As Range
        Dim arrIndex As Long
        Dim arrData() As Variant
        ReDim arrData(1 To 4, 1 To Rows.Count)
        
        With Application
            iCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        On Error Resume Next
        
        strTemp = InputBox("Enter the Start Date", "Start Date")
        If Trim(strTemp) = vbNullString Then GoTo ExitMacro
        StartDate = CDate(strTemp)
        If StartDate = 0 Then
            MsgBox """" & strTemp & """ is an invalid date." & Chr(10) & "Exiting Macro"
            GoTo ExitMacro
        End If
        
        strTemp = InputBox("Enter the End Date. Must be on after " & StartDate, "End Date")
        If Trim(strTemp) = vbNullString Then GoTo ExitMacro
        EndDate = CDate(strTemp)
        If EndDate = 0 Then
            MsgBox """" & strTemp & """ is an invalid date." & Chr(10) & "Exiting Macro"
            GoTo ExitMacro
        End If
        
        If EndDate < StartDate Then
            MsgBox EndDate & " is prior to " & StartDate & "." & Chr(10) & "Exiting Macro"
            GoTo ExitMacro
        End If
        
        For Each ws In ActiveWorkbook.Sheets
            If ws.Name <> ActiveSheet.Name Then
                Set rngDate = ws.Columns("A").Find("Date")
                If Not rngDate Is Nothing Then
                    With Range(rngDate, ws.Cells(Rows.Count, "A").End(xlUp))
                        .AutoFilter 1, ">=" & StartDate, xlAnd, "<=" & EndDate
                        Set rngVis = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
                        If Not rngVis Is Nothing Then
                            Set rngName = ws.Columns("A").Find("Name")
                            Set rngID = ws.Columns("A").Find("ID")
                            For Each VisCell In rngVis
                                arrIndex = arrIndex + 1
                                arrData(1, arrIndex) = VisCell.Value
                                arrData(2, arrIndex) = rngName.Offset(, 1).Value
                                arrData(3, arrIndex) = rngID.Offset(, 1).Value
                                arrData(4, arrIndex) = VisCell.Offset(, 1).Value
                            Next VisCell
                            Set rngVis = Nothing
                        End If
                        .AutoFilter
                    End With
                End If
            End If
        Next ws
        
        If arrIndex = 0 Then
            MsgBox "No matches found."
            GoTo ExitMacro
        Else
            ReDim Preserve arrData(1 To 4, 1 To arrIndex)
            Range("B8", Cells(Rows.Count, "E")).ClearContents
            Range("B8:E8").Resize(arrIndex).Value = Application.Transpose(arrData)
        End If
    
    ExitMacro:
        With Application
            .Calculation = iCalc
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Last edited by tigeravatar; 02-21-2012 at 07:18 PM.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Hi,

    Thanks for your assistance on this.
    Will the code have to be modified to run correctly on a 2002 version of excel?
    I just noticed the machines at work are running that, and the modded workbook you upped isn't working right.

    Thanks,

    Ed

  4. #4
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Hi Again,

    I'm at home now, and I've tried the workbook you modified.
    Unfortunately its not working for me.
    It just hangs unresponsive or reports back no matches found, even if there are?

    Any other ideas?

    Once again, I am extremely grateful for any help.

    Ed

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Date range look up and copy/paste coding

    You can save the file as a .xls and it will be a 2003 version, no adjustments needed. As a test (not in this uploaded file because it exceeded 9 MB in size), I also created 200 worksheets with each having between 20 and 1100 rows and then ran the macro on it and it ran quickly (less than 3 seconds) and properly. If you can upload a workbook that is having issues, I'll take a look at it to try to find where the problem is.

  6. #6
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Hi,

    I have downloaded and run the workbook in your last post and it was still not working correctly.
    To make things simpler, I have adjusted the dates in the named sheets - 01/01/2012, 02/02/2012, 03/03/2012 and 04/04/2012.
    If I do a search for 01/01/2012 - 02/01/2012, it should only provide the first date result, but it doesn't.
    It just hangs..

    If I then break the code with the esc. key and go to debug, it is always stuck in this section

    arrIndex = arrIndex + 1
          arrData(1, arrIndex) = VisCell.Value
          arrData(2, arrIndex) = rngName.Offset(, 1).Value
          arrData(3, arrIndex) = rngID.Offset(, 1).Value
          arrData(4, arrIndex) = VisCell.Offset(, 1).Value
          Next VisCell
    I thought it may have been a date format issue, but I have checked the format of the date in all relevant cells and they match (dd/mm/yyyy)

    Not sure why it works on your machine, but not on any of mine?

    I have attached the simplified workbook for you to look at.

    Many thanks for your time on this matter.

    Ed
    Attached Files Attached Files

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Date range look up and copy/paste coding

    mungel,

    I think I found the problem. For some reason, it wasn't calculating rngVis properly if there was only a single result. I have made a slight modification to the code which appears to have corrected that issue:

    Changed this line:
    If Not rngVis Is Nothing  Then

    To this:
    If Not rngVis Is Nothing And InStr(rngVis.Address, "$1") = 0 Then

    Full code with change:
    Sub tgr()
        
        Dim iCalc As Integer
        Dim strTemp As String
        Dim StartDate As Date
        Dim EndDate As Date
        Dim ws As Worksheet
        Dim rngName As Range
        Dim rngID As Range
        Dim rngDate As Range
        Dim rngVis As Range
        Dim VisCell As Range
        Dim arrIndex As Long
        Dim arrData() As Variant
        ReDim arrData(1 To 4, 1 To Rows.Count)
        
        With Application
            iCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        On Error Resume Next
        
        strTemp = InputBox("Enter the Start Date", "Start Date")
        If Trim(strTemp) = vbNullString Then GoTo ExitMacro
        StartDate = CDate(strTemp)
        If StartDate = 0 Then
            MsgBox """" & strTemp & """ is an invalid date." & Chr(10) & "Exiting Macro", , "Invalid Date"
            GoTo ExitMacro
        End If
        
        strTemp = InputBox("Enter the End Date. Must be on or after " & StartDate, "End Date")
        If Trim(strTemp) = vbNullString Then GoTo ExitMacro
        EndDate = CDate(strTemp)
        If EndDate = 0 Then
            MsgBox """" & strTemp & """ is an invalid date." & Chr(10) & "Exiting Macro", , "Invalid Date"
            GoTo ExitMacro
        End If
        
        If EndDate < StartDate Then
            MsgBox EndDate & " is prior to " & StartDate & "." & Chr(10) & "Exiting Macro", , "Invalid Dates"
            GoTo ExitMacro
        End If
        
        For Each ws In ActiveWorkbook.Sheets
            If ws.Name <> ActiveSheet.Name Then
                Set rngDate = ws.Columns("A").Find("Date")
                If Not rngDate Is Nothing Then
                    With Range(rngDate, ws.Cells(Rows.Count, "A").End(xlUp))
                        .AutoFilter 1, ">=" & StartDate, xlAnd, "<=" & EndDate
                        Set rngVis = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
                        If Not rngVis Is Nothing And InStr(rngVis.Address, "$1") = 0 Then
                            Set rngName = ws.Columns("A").Find("Name")
                            Set rngID = ws.Columns("A").Find("ID")
                            For Each VisCell In rngVis
                                arrIndex = arrIndex + 1
                                arrData(1, arrIndex) = VisCell.Value
                                arrData(2, arrIndex) = rngName.Offset(, 1).Value
                                arrData(3, arrIndex) = rngID.Offset(, 1).Value
                                arrData(4, arrIndex) = VisCell.Offset(, 1).Value
                            Next VisCell
                            Set rngVis = Nothing
                        End If
                        .AutoFilter
                    End With
                End If
            End If
        Next ws
        
        If arrIndex = 0 Then
            MsgBox "No matches found.", , "No Matches"
            GoTo ExitMacro
        Else
            ReDim Preserve arrData(1 To 4, 1 To arrIndex)
            Range("B8", Cells(Rows.Count, "E")).ClearContents
            Range("B8:E8").Resize(arrIndex).Value = Application.Transpose(arrData)
        End If
    
    ExitMacro:
        With Application
            .Calculation = iCalc
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub

  8. #8
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding



    Ok, so I broke it again, but I swear I'm not messing with the code.

    I have adjusted the dates on the sheets, so they are all between 02/01/2012 and 10/01/2012 (dd/mm/yyyy)
    Now when I do a search for between 01/01/2012 and 05/01/2012 it should just show a few entries.
    But instead it shows them all and changes the dates to 01/XX/2012?

    Also, if I search for 03/01/2012 and 06/01/2012, it goes mental and displays one entry for each sheet, without date or detail?

    I'm so sorry, maybe what I'm asking is too much for excel to handle?

    Here is the workbook with the dates adjusted..

    Thanks again for your help.
    Attached Files Attached Files

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Date range look up and copy/paste coding

    mungel,

    I found the issue that was causing the second the problem and have corrected it. I was unable to duplicate the first issue you mentioned.

  10. #10
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Hi again,

    I think I have figured out the problem, but I have no clue how to remedy it.
    It has to do with the date format.
    Here in the UK the standard date format is dd/mm/yyyy and all the worksheets will have the date formatted that way.
    The macro you wrote is reading the date range entered by the user as mm/dd/yyyy and thus producing incorrect results.
    To test this, I changed the dates in the worksheets back to between 01 January 2012 and 10 January 2012 and did a search range of 01/01/2012 and 03/01/2012.
    This search range displayed every result, as it was searching from 01 January 2012 to 01 March 2012 and not 01 January to 03 January as I thought it was.
    A search range of 01/01/2012 and 01/03/2012 worked correctly, only producing a few results.

    So is there a way to force the macro to accept a dd/mm/yyyy format?
    This is the format here in the UK and everyone entering dates or running searches will be entering dates in this format.

    As a side issue, and out of curiosity, I changed the format of the date cells to dd/mmm/yyyy so that all the dates were shown in full (01 January 2012 to 10 January 2012)
    I also changed the format of the destination cells on the weekly worksheet to match this.
    When I did a search for 01/01/2012 to 03/01/2012 (January to March in US format) it produced all the results (as expected) but changed the dates in the destination cells.
    So even though the date was displayed as 06 January 2012 on the worksheet, when copied into the weekly worksheet, it became 01 June 2012.
    This change happened to every result, making them all incorrect, even though they were displayed correctly before being copied.

    Attached is the very workbook I was just using. You should be able to replicate the results above and see the issues I am highlighting.

    Once again, and probably not for the last time, I am expressing my eternal gratitude for your assistance on this matter.
    I may end up buying you a crate of beer or something..

    Thanks,

    Ed
    Last edited by mungel; 02-24-2012 at 04:23 AM. Reason: forgot the attachment.. its been a long day

  11. #11
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Date range look up and copy/paste coding

    mungel,

    Excel should use whatever your computer's Regional and Date settings are set to. I changed mine to English (United Kingdom) and it worked as expected with d/m/y date format entries.

  12. #12
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    It should yes, but it's not.
    My computers are set to UK, but the macro is changing the output to US.
    I don't understand it.
    In the last attachment you can see the results, not one of those dates is used in any of the worksheets, but the macro produced them?

  13. #13
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Date range look up and copy/paste coding

    THere are two things I would change:
    1. Use CLng to convert the date criteria when autofiltering.
    2. Use:
    arrData(1, arrIndex) = VisCell.Value2
                                arrData(2, arrIndex) = rngName.Offset(, 1).Value2
                                arrData(3, arrIndex) = rngID.Offset(, 1).Value2
                                arrData(4, arrIndex) = VisCell.Offset(, 1).Value2
    to avoid date issues when populating the array.
    Good luck.

  14. #14
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Quote Originally Posted by OnErrorGoto0 View Post
    THere are two things I would change:
    1. Use CLng to convert the date criteria when autofiltering.
    2. Use:
    arrData(1, arrIndex) = VisCell.Value2
                                arrData(2, arrIndex) = rngName.Offset(, 1).Value2
                                arrData(3, arrIndex) = rngID.Offset(, 1).Value2
                                arrData(4, arrIndex) = VisCell.Offset(, 1).Value2
    to avoid date issues when populating the array.
    That's so nearly it...
    The above code has fixed the date issue when it's pasted. Thank you so much.

    The only issue I have now is that I have to enter the date range US format to get the correct output. Any ideas how I can get the input boxes to accept the UK format?

    So close to getting it right. Thanks for all the help.

  15. #15
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Date range look up and copy/paste coding

    Cdate should always apply your regional settings so if you enter a UK date string, that is what should be output. Can you post the exact code, preferably in a sample workbook so we can verify data too, and a specific example failing for you?

  16. #16
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    The attached workbook is the most recent copy. I have included the code that you suggested and the dates now copy and display correctly (thanks)

    The data in the worksheets is dated between 01/01/2012 and 10/01/2012 (that's 1st Jan to 10th Jan to me).
    When the macro is run, and I enter a range of 01/01/2012 to 04/01/2012 (that's 1st Jan to 4th Jan), it assumes the US format and displays all ten results, when it should have only displayed a few.(as it thinks I'm searching 1st Jan to 1st April)

    If I enter a range of 01/01/2012 to 01/04/2012 (the US format), it displays the correct results.(1st Jan to 4th Jan), but that range to me and anyone else that will use it is 1st Jan to 1st April.

    I absolutely need it to accept the input as dd/mm/yyyy.

    Hope this makes sense?

    Many thanks,

    Ed.

  17. #17
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    Hi,

    Bumping this back up for a bit of help, rather than starting a new thread.

    I have implemented the code into a working model but it has developed an issue.
    If I do a search for the dates 01/02/2012 to 22/07/2012, it should search every sheet and display every result(as the data contained in the sheets is between these dates), but for some reason it is only displaying some of the results. It seems to omit any results from sheets that have more than one entry.

    Anyone able to help me with this small issue?

    Attached is the workbook - Macro 2 is the code which is not quite right.

    Thanks in advance for any help on this..
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    05-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    33

    Re: Date range look up and copy/paste coding

    That's the ticket.

    Thank you guys, both so much.

    I just hope that I might be able to help someone else in the future, but as you can see, I have along way to go with my knowledge on VB.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1