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