Results 1 to 7 of 7

Thread: Macro to Filter data based on 4 Criteria's and move filtered data into another sheet

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Macro to Filter data based on 4 Criteria's and move filtered data into another sheet

    I have worksheet which consists of multiple columns of data, in this worksheet I have below 4 columns on which I need to apply following filter logic and if the filter logic matches then I need to move those rows of data into a different worksheet.

    Columns on which the criteria needs to be applied: (Note: There are multiple other columns in the actual worksheet but in the sample data I have just included the ones which are required to apply the criteria)
    Column 1: Client Name
    Column 2: Price
    Column 3: Quantity
    Column 4: Label

    Below is my filter Criteria:
    Label = Trade Alloc or Trade Exec
    Quantity = Sumtotal of Trade Alloc = Sumtotal of Trade Exec rows
    Price = Same betweenTrade Alloc and Trade Exec .i.e. Trade Alloc = Trade Exec Rows
    Client Name = Same between Trade Alloc and Trade Exec .i.e. Trade Alloc = Trade Exec Rows

    Attached is the sample data.
    First Sheet: Sample Rec Data
    Second Sheet: Sample Filtered Data


    Now I have below SQL Statement from my MS Access Database, can we incorporate similar thing in ms excel macro. but in the below case it also excludes the rows which shouldn't be captured in output for e.g. if the total sum of Qty column for Trade Exec is 10 and Total Sum of Trade Alloc is 22 then Macro should take only those rows for which the sum total of Trade Alloc label items is 10 and copy the same to next sheet.

    Code:
    SELECT [Sample Rec Data].[Name of Client], [Sample Rec Data].Price, Sum([Sample Rec Data].Qty) AS SumOfQty, [Sample Rec Data].Label
    FROM [Sample Rec Data]
    GROUP BY [Sample Rec Data].[Name of Client], [Sample Rec Data].Price, [Sample Rec Data].Label
    HAVING ((([Sample Rec Data].Label)='TRADE ALLOC' Or ([Sample Rec Data].Label)='TRADE EXEC'));
    I have got the below code so far but need help to incorporate the above sql statement into below macro.

    Code:
    Sub sbADOExample()
    
    'Using Microsoft Excel 2013
    'Reference Microsoft ActiveX Data Objects 2.8 Library
    
    Dim sSQLQry As String
    Dim ReturnArray
    
    Dim Conn As New ADODB.Connection
    Dim mrs As New ADODB.Recordset
    
    Dim DBPath As String, sconnect As String
    
    
    
    DBPath = ThisWorkbook.FullName
    
    
    sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
    
    
    
    Conn.Open sconnect
        
        'sSQLSting = "SELECT * From [Sample Rec Data$]"
       sSQLSting = "SELECT [Sample Rec Data$].[Name of Client], [Sample Rec Data$].Price,[Sample Rec Data].Label, SUM([Sample Rec Data].[Qty]) " & _
             "FROM [Sample Rec Data$]" & _
             "GROUP BY [Sample Rec Data$].[Name of Client], [Sample Rec Data$].Price, [Sample Rec Data$].Label"
             
            
        mrs.Open sSQLSting, Conn
         
            Sheets("Sample Filtered Data").Range("A2").CopyFromRecordset mrs
       
        mrs.Close
    
    
    Conn.Close
    
    End Sub
    Attached Files Attached Files
    Last edited by abhay_547; Jun 24th, 2018 at 11:08 PM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width