Autofilter VBA Help!

longytravel

Board Regular
Joined
Aug 2, 2011
Messages
68
Afternoon,

Wondering if someone could help.

I have the following code that I have put together with the help from various people

I would like to remove the Autofilter element - i am simply looking to have a userform which will search for infomration between two dates. It will then copy and paste the info from between the two dates to a new sheet which i have labelled 'Reports'.

By having the autofilter element in it filters the info of the 'data' sheet which is not needed

Look forward to help as ever!

Code:
Private Sub CommandButton1_Click()
    Date1 = Me.Date10.Text
    Date2 = Me.Date20.Text
    With Sheets("data")
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        With .Range("B1")
            .AutoFilter
            .AutoFilter Field:=2, Criteria1:=">=" & Date1, Operator:=xlAnd _
            , Criteria2:="<=" & Date2
        End With
        .Range("B1:B" & lr).EntireRow.Copy Sheets("reports").Range("A1")
    End With
End Sub
Private Sub Date10_AfterUpdate()
If Not IsDate(Me.Date10.Value) Then
    Me.Date10.Text = ""
    MsgBox "please type a valid date!"
    Exit Sub
End If
End Sub
Private Sub Date20_AfterUpdate()
If Not IsDate(Me.Date20.Value) Then
    Me.Date20.Text = ""
    MsgBox "please type a valid date!"
    Exit Sub
End If
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I believe the Autofilter IS the way to do this. The only thing missing from your first macro is turning the filter off when you're done.
Code:
Private Sub CommandButton1_Click()
    Date1 = Me.Date10.Text
    Date2 = Me.Date20.Text
    With Sheets("data")
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        .Range("B1").AutoFilter
        .Range("B1").AutoFilter Field:=2, Criteria1:=">=" & Date1, Operator:=xlAnd, _
            Criteria2:="<=" & Date2
        .Range("B1:B" & LR).EntireRow.Copy Sheets("reports").Range("A1")
        .AutoFilterMode = False
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top