Results 1 to 7 of 7

Thread: PLEASE help me with a VBA macro to filter data with mutiple criteria

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    If you don't really want a filter, a simple loop would be easier I think


    Option Explicit
    Sub Clearme()
    '
    ' Clearme Macro
    ' Clear all filtered data
    '
    ' Keyboard Shortcut: Ctrl+Shift+C
    '
        Worksheets("Interface").Range("C9:I40").ClearContents
        
    End Sub
    Sub Filterme()
        
        Dim wsData As Worksheet, wsInterface As Worksheet
        
        Dim dtStart As Date, dtEnd As Date
        Dim sCustomer As String, sProduct As String
        Dim iInvoice As Long
        Dim iOut As Long, iDataRow As Long
        
        Set wsData = Worksheets("Data")
        Set wsInterface = Worksheets("Interface")
        
        Application.ScreenUpdating = False
        
        With wsInterface
            .Range("C9:I40").ClearContents
            dtStart = .Range("C6").Value
            dtEnd = .Range("D6").Value
            sCustomer = LCase(.Range("E6").Value)
            iInvoice = .Range("F6").Value
            sProduct = LCase(.Range("G6").Value)
        End With
    
        If CLng(dtStart) = 0 Then dtStart = DateSerial(2000, 1, 1)
        If CLng(dtEnd) = 0 Then dtEnd = DateSerial(2099, 12, 31)
    
        iOut = 9
        iDataRow = 5
        
        With wsData
            'Date    Invoice Number  Customer    Unit price  Product Name    Quantity    Total
            Do While Len(.Cells(iDataRow, 4).Value) > 0
                If .Cells(iDataRow, 4).Value < dtStart Then GoTo NextDataRow
                If .Cells(iDataRow, 4).Value > dtEnd Then GoTo NextDataRow
                If iInvoice > 0 Then
                    If .Cells(iDataRow, 5).Value <> iInvoice Then GoTo NextDataRow
                End If
                If Len(sCustomer) > 0 Then
                    If LCase(.Cells(iDataRow, 6).Value) <> sCustomer Then GoTo NextDataRow
                End If
                If Len(sProduct) > 0 Then
                    If LCase(.Cells(iDataRow, 8).Value) <> sProduct Then GoTo NextDataRow
                End If
                            
                Call .Cells(iDataRow, 4).Resize(1, 7).Copy(wsInterface.Cells(iOut, 3))
                            
                iOut = iOut + 1
                
    NextDataRow:
                iDataRow = iDataRow + 1
            
            Loop
        End With
         
        Application.ScreenUpdating = True
         
        MsgBox "Done"
         
         
    End Sub

    Attached is my update if you're interested

    You should add error checking, e.g. is Start really a date, etc.
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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