+ Reply to Thread
Results 1 to 18 of 18

Date range look up and copy/paste coding

Hybrid View

  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

    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

  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,

    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

  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 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

+ 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