Hi all
I'm pulling my hair out with this.
I cannot get the auto filter to work, I'm sure im missing something but cant seem to see what????
The code below is run from a userform, i've split it into two subs which I'll combine later. I've got all the dates into mm/dd/yyyy format as from past experience it seems to work better in this format.
I just cant get it to filter the two columns in the date range. Any help would preserve what i have left of my hair.
Kind regards
Paul
I'm pulling my hair out with this.
I cannot get the auto filter to work, I'm sure im missing something but cant seem to see what????
The code below is run from a userform, i've split it into two subs which I'll combine later. I've got all the dates into mm/dd/yyyy format as from past experience it seems to work better in this format.
I just cant get it to filter the two columns in the date range. Any help would preserve what i have left of my hair.
Kind regards
Paul
Code:
Private Sub CommandButton1_Click()
' clear sheet2 ready
Dim Lastrow1 As Long
Lastrow1 = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
With Sheet2
.Select
Range("A1:D" & Lastrow1).ClearContents
End With
'delete unwanted columns sheet1
With Sheet1
.Select
Columns("A:E").EntireColumn.Delete
Columns("C:E").EntireColumn.Delete
End With
'filter dates
Sheet2.Range("D1") = TextBox1.Value 'from date
Sheet2.Range("B1") = TextBox2.Value 'end date
Sheet2.Range("B1").NumberFormat = "mm/dd/yyyy"
Sheet2.Range("D1").NumberFormat = "mm/dd/yyyy"
Sheet2.Range("A1").Value = "End Date"
Sheet2.Range("C1").Value = "Start Date"
Dim Lastrow As Long
Lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Sheet1.Range("A1:A" & Lastrow).NumberFormat = "mm/dd/yyyy"
sortoutfordates
End Sub
Sub sortoutfordates()
Dim lDateFrom As Long
Dim lDateTo As Long
Dim Lastrow As Long
Lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
DateTo = Sheet2.Range("B1")
DateFrom = Sheet2.Range("D1")
With Sheet1
.AutoFilterMode = False
With Sheet1
Range("A1:B" & Lastrow).AutoFilter Field:=1, Criteria1:=">=" & DateFrom, Operator:=xlAnd, Criteria2:="<=" & DateTo
Range("A1:B" & Lastrow).Copy
End With
End With
With Sheet2
.Select
With Range("A3")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Select
End With
End With
Application.CutCopyMode = False
Unload Me
End Sub