+ Reply to Thread
Results 1 to 7 of 7

Filter a sheet and copy results to another sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    Pilsen, CZ
    MS-Off Ver
    Excel 2010
    Posts
    5

    Filter a sheet and copy results to another sheet

    Hi
    I was wondering if someone could help me.
    I need to add two more criteria (from date and to date) filter
    the code: http://www.vbaexpress.com/kb/getarticle.php?kb_id=786

    Thanks very much in advance

  2. #2
    Registered User
    Join Date
    03-26-2012
    Location
    India
    MS-Off Ver
    excel 2007
    Posts
    21

    Re: Filter a sheet and copy results to another sheet

    Do you want all the filter criterion to be taken as an input via an input box or they are going to be value in some cell?

  3. #3
    Registered User
    Join Date
    10-03-2012
    Location
    Pilsen, CZ
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Filter a sheet and copy results to another sheet

    ...via an input box.

  4. #4
    Registered User
    Join Date
    03-26-2012
    Location
    India
    MS-Off Ver
    excel 2007
    Posts
    21

    Re: Filter a sheet and copy results to another sheet

    try this in the user form now insert 3 textfields instead of one
    Option Explicit 
    Function FilterAndCopy(rng As Range, Choice1 As String, Choice2 as string, choice3 as string) 
         
        Dim FiltRng As Range 
         'Clear Contents to show just new search data
        Worksheets("Sheet2").Cells.ClearContents 
         'Set the column to filter (In This Case 1 or A)
         'Change as required
        rng.AutoFilter Field:=1, Criteria1:=Choice1
     'Set the column to filter (In This Case 2 or B)
         'Change as required
    rng.AutoFilter Field:=2, Criteria1:=Choice2
    'Set the column to filter (In This Case 3 or c)
         'Change as required
    rng.AutoFilter Field:=1, Criteria1:=Choice3
        On Error Resume Next 
        Set FiltRng = rng.SpecialCells(xlCellTypeVisible).EntireRow 
        On Error Goto 0 
         
         'Copy Data across to sheet 2
        FiltRng.Copy Worksheets("Sheet2").Range("A1") 
         'Display Data
        Worksheets("Sheet2").Select 
        Range("A1").Select 
        Set FiltRng = Nothing 
    End Function 
     
    Sub formshow() 
         'Show Search Form
        UserForm1.Show 
    End Sub 
     
     '*****************************************************************
     'In a userform
     
    Option Explicit 
     
    Private Sub CommandButton1_Click() 
        Dim rng As Range 
         
         'Set Error Handling
    On Error Goto ws_exit: 
        Application.EnableEvents = False 
         'Set Range
        Set rng = ActiveSheet.UsedRange 
         'Cancel if no value entered in textbox
    If TextBox1.Value = "" or TextBox2.Value = "" or TextBox3.Value = "" Then Goto ws_exit: 
         'Call function Filterandcopy
        FilterAndCopy rng, TextBox1.Value, TextBox2.Value, TextBox3.Value
        rng.AutoFilter 
         'Exit sub
    ws_exit: 
        Set rng = Nothing 
        Application.EnableEvents = True 
        Unload Me 
    End Sub 
     
    Private Sub CommandButton2_Click() 
         'Cancel Button
        Unload Me 
    End Sub
    CLICK the STAR icon on the left side below their user name to say thank you to those who had helped you. Thanks.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.
    To undo, select Thread Tools-> Mark thread as Unsolved.

  5. #5
    Registered User
    Join Date
    10-03-2012
    Location
    Pilsen, CZ
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Filter a sheet and copy results to another sheet

    thank you very much
    but, It's possible that....

    Textbox1 = text (abc) = Column A
    TextBox2 = date from (1.10.12) = Column B
    Textbox3 = date to (4.10.12) = Column B
    result:
    textBox1 and (TextBox2 (up) to textbox3) => Sheet 2

    Thanks very much

  6. #6
    Registered User
    Join Date
    03-26-2012
    Location
    India
    MS-Off Ver
    excel 2007
    Posts
    21

    Re: Filter a sheet and copy results to another sheet

    Pl. use this code
    Option Explicit 
    Function FilterAndCopy(rng As Range, Choice1 As String, Choice2 as string, choice3 as string) 
         
        Dim FiltRng As Range 
         'Clear Contents to show just new search data
        Worksheets("Sheet2").Cells.ClearContents 
         'Set the column to filter (In This Case 1 or A)
         'Change as required
        rng.AutoFilter Field:=1, Criteria1:=Choice1
     'Set the column to filter (In This Case 2 or B)
         'Change as required
    rng.AutoFilter Field:=2, Criteria1:= _
            ">=" & choice2, Operator:=xlAnd, Criteria2:="<=" & c
    
        On Error Resume Next 
        Set FiltRng = rng.SpecialCells(xlCellTypeVisible).EntireRow 
        On Error Goto 0 
         
         'Copy Data across to sheet 2
        FiltRng.Copy Worksheets("Sheet2").Range("A1") 
         'Display Data
        Worksheets("Sheet2").Select 
        Range("A1").Select 
        Set FiltRng = Nothing 
    End Function 
     
    Sub formshow() 
         'Show Search Form
        UserForm1.Show 
    End Sub 
     
     '*****************************************************************
     'In a userform
     
    Option Explicit 
     
    Private Sub CommandButton1_Click() 
        Dim rng As Range 
         
         'Set Error Handling
    On Error Goto ws_exit: 
        Application.EnableEvents = False 
         'Set Range
        Set rng = ActiveSheet.UsedRange 
         'Cancel if no value entered in textbox
    If TextBox1.Value = "" or TextBox2.Value = "" or TextBox3.Value = "" Then Goto ws_exit: 
         'Call function Filterandcopy
        FilterAndCopy rng, TextBox1.Value, TextBox2.Value, TextBox3.Value
        rng.AutoFilter 
         'Exit sub
    ws_exit: 
        Set rng = Nothing 
        Application.EnableEvents = True 
        Unload Me 
    End Sub 
     
    Private Sub CommandButton2_Click() 
         'Cancel Button
        Unload Me 
    End Sub
    CLICK the STAR icon on the left side below their user name to say thank you to those who had helped you. Thanks.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.
    To undo, select Thread Tools-> Mark thread as Unsolved.

  7. #7
    Registered User
    Join Date
    10-03-2012
    Location
    Pilsen, CZ
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Filter a sheet and copy results to another sheet

    thank you very much
    Last edited by dixiV; 10-04-2012 at 01:37 PM.

+ 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