+ Reply to Thread
Results 1 to 2 of 2

Select rows with matching date in column

Hybrid View

  1. #1
    Registered User
    Join Date
    10-04-2010
    Location
    Adelaide Australia
    MS-Off Ver
    Excel 2003
    Posts
    1

    Select rows with matching date in column

    I am looking to be able to have an input box display for the user to enter a date (or date range) and then have it select only rows that have a matching date (or dates) in column AD. I have found code that I thought might put me on the right track, but it does not seem to work correctly at the moment. It does not select rows and I am concerned I might be having some issue because of date format being entered into the user input box. I am in Australia so our date format is dd/mm/yy by default
    CODE IN FORM:
    '========================================================================
    '- USERFORM : SELECT 2 DATES AND HIDE NON-MATCHING WORKSHEET ROWS
    '- USING "Microsoft Date and Time Picker Control" MSCOMCT2.OCX
    '- (Rightclick userform toolbox/Additional Controls)
    '========================================================================
    '- requires 2 Date and Time Picker controls (for 'From Date' & 'To date')
    '- ensure all control names match those in the code
    '- Cannot do PrintPreview from form code so use form.Tag to indicate OK
    '- Code for macro module shown at the end.
    '- Brian Baulsom May 2007
    '========================================================================
    Dim MyDate1 As Date
    Dim MyDate2 As Date
    Dim ws As Worksheet
    Dim MyRow As Long
    Dim LastRow As Long
    Dim ExcludeRange As Range
    Dim MyCell As Range
    Dim RowCount As Long
    '========================================================================
    '- OK BUTTON CODE
    '========================================================================
    Private Sub OKbutton_Click()
        MyDate1 = DTPicker1.Value
        MyDate2 = DTPicker2.Value
        Set ExcludeRange = Nothing
        '--------------------------------------------------------------------
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        Set ws = Worksheets("ODO PC Assets")
        ws.Rows.EntireRow.Hidden = False
        ws.Range("AD1").Sort Key1:=Range("AD2"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        LastRow = ws.Range("A65536").End(xlUp).Row
        '-------------------------------------------------------------------
        '- MAIN LOOP
        For MyRow = 2 To LastRow
            Set MyCell = ws.Cells(MyRow, 1)
            If MyCell.Value < MyDate1 Or MyCell.Value > MyDate2 Then
                If ExcludeRange Is Nothing Then
                    '- first matching cell
                    Set ExcludeRange = MyCell
                Else
                    '- add subsequent matching cells to the range
                    Set ExcludeRange = Union(ExcludeRange, MyCell)
                End If
            End If
        Next
        '-----------------------------------------------------------------
        '- Hide all rows in ExcludeRange
        On Error Resume Next
        RowCount = LastRow - ExcludeRange.Cells.Count
        If Err.Number <> 0 Then
            MsgBox ("No records apply.")
            Me.Tag = "Invalid"
        ElseIf RowCount = 1 Then    ' column heading row
            MsgBox ("No records outside dates selected.")
            Me.Tag = "Invalid"
        Else
            ExcludeRange.EntireRow.Hidden = True
            MsgBox ("Valid rows : " & RowCount)
            Me.Tag = "Valid"
        End If
        '----------------------------------------------------------------
        '- FINISH
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        Application.Goto ws.Range("A1"), Scroll:=True
        Me.Hide
    End Sub
    '--------------------------------------------------------------------
    
    '=============================
    '- CANCEL BUTTON CODE
    '=============================
    Private Sub CancelButton_Click()
        Me.Hide
    End Sub
    
    '=================================================
    '- *** THIS CODE GOES IN THE MACRO MODULE ***
    '=================================================
    Sub BETWEEN_DATES()
        DisposeDateSelect.Show
        If DisposeDateSelect.Tag = "Valid" Then
            ActiveSheet.PrintPreview
        End If
        Unload DisposeDateSelect
    End Sub
    '================================================
    Any help would be greatly appreciated.
    Last edited by Leith Ross; 10-05-2010 at 09:42 PM. Reason: Added Code Tags

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Select rows with matching date in column

    Hi georgeu,
    welcome to the forum, why dont you just filter the list?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

+ 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