+ Reply to Thread
Results 1 to 18 of 18

Date range look up and copy/paste coding

Hybrid View

mungel Date range look up and... 02-21-2012, 04:58 PM
tigeravatar Re: Date range look up and... 02-21-2012, 07:08 PM
mungel Re: Date range look up and... 02-21-2012, 10:02 PM
mungel Re: Date range look up and... 02-22-2012, 03:02 AM
tigeravatar Re: Date range look up and... 02-22-2012, 12:16 PM
mungel Re: Date range look up and... 02-22-2012, 04:06 PM
tigeravatar Re: Date range look up and... 02-22-2012, 05:16 PM
mungel Re: Date range look up and... 02-23-2012, 04:31 AM
tigeravatar Re: Date range look up and... 02-23-2012, 11:44 AM
mungel Re: Date range look up and... 02-24-2012, 04:21 AM
tigeravatar Re: Date range look up and... 02-24-2012, 11:52 AM
mungel Re: Date range look up and... 02-24-2012, 02:41 PM
OnErrorGoto0 Re: Date range look up and... 02-24-2012, 03:45 PM
mungel Re: Date range look up and... 02-24-2012, 07:27 PM
OnErrorGoto0 Re: Date range look up and... 02-24-2012, 07:42 PM
mungel Re: Date range look up and... 02-24-2012, 09:46 PM
mungel Re: Date range look up and... 07-23-2012, 09:23 AM
mungel Re: Date range look up and... 02-27-2012, 02:40 PM
  1. #1
    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.
    Hope that helps,
    ~tigeravatar

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

  2. #2
    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

  3. #3
    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

  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



    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

+ 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