Consulting

Results 1 to 4 of 4

Thread: Filter based on 3 OR conditions and cut paste the same on different sheet

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location

    Filter based on 3 OR conditions and cut paste the same on different sheet

    Hello Everybody,

    Looking for a macro code which will Filter the data of a column based on 3 OR conditions and cut paste the same in new sheet named "Kont".

    I have recorded below code but unable to add third OR condition i.e. "=*GHI*".
    Kindly assist me on the same.


    ActiveSheet.Range("$A$1:$R$7322").AutoFilter Field:=9, Criteria1:="=*ABC*" _
    , Operator:=xlOr, Criteria2:="=*DEF*"


    Regards,
    Shan

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_54323_AutoFilter_Three_Or_More_Criteria()
    
    Dim FilterCrit
    Dim i As Long
    
    FilterCrit = Array("*ABC*", "*DEF*", "*GHI*")
    
    With Worksheets("MySheet") 'change MySheet to suit
        For i = LBound(FilterCrit) To UBound(FilterCrit)
            .Cells(1).AutoFilter Field:=9, Criteria1:="=" & FilterCrit(i)
            If .AutoFilter.Range.Rows.Count > 1 Then 'check if there are any filtered rows
                'code with filtered rows here
                
                'example: copy and delete filtered rows
                '.UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Copy Destination:=Worksheets("MySheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
                '.UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
            
            End If
        Next
    End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would so it by adding a column and building a formula that I filter on

        With ActiveSheet
        
            .Columns(10).Insert
            .Range("J1").Value = tmp
            .Range("$J$2:$J$7322").Formula = "=SUM(COUNTIF(I2,{""*ABC*"",""*DEF*"",""*GHI*""}))>0"
            .Range("$A$1:$R$7322").AutoFilter Field:=10, Criteria1:="TRUE"
            'do your stuff
            
            .Columns(10).Delete 'tidy-up afterwards
        End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location
    Thank you Sir,

    The code is working as desired.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •