How to consolidate similar entries in a sorted list without output to a worksheet using VBA/Excel

后端 未结 3 714
不思量自难忘°
不思量自难忘° 2020-12-18 17:11

I have an array which stores it\'s values in a sorted list. I have been using this sorted list to organise data, by date in several other spreadsheets.

My source dat

3条回答
  •  有刺的猬
    2020-12-18 17:55

    Here is shorter lazier version that will aggregate the example data into a 2D array, but it assumes that A6:E6 has the same header names as in your example:

    Dim arr(), rs As Object: Set rs = CreateObject("ADODB.Recordset")
    
    rs.Open "Select [Delivered to:], Count(*), Sum([No# Pieces:]), " & _
        "Sum([Weight:]), Format(Sum([Cost:]),'$0.00') " & _
        "From ( SELECT * From [January$A6:E207] Union All " & _
        "       SELECT * From [February$A6:E207] ) " & _
        "Where [Delivered to:] > ''  Group By [Delivered to:]", _
        "Provider=MSDASQL;DSN=Excel Files;DBQ=" & ThisWorkbook.FullName
    
    If Not rs.EOF Then arr = rs.GetRows ': For Each i In arr: Debug.Print i & " ";: Next
    rs.Close: Set rs = Nothing
    

    If there are no header cells, this alternative version needs the ACE Provider to be installed (comes with Access 2007 and above, or can be downloaded and installed separately)

    rs.Open "Select F2, Count(*), Sum(F3), Sum(F4), Format(Sum(F5),'Currency') " & _
        "From ( SELECT * From [January$A6:E207] Union All " & _
        "       SELECT * From [February$A6:E207]          )  Where F2 > ''  Group By F2", _
        "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & ThisWorkbook.FullName ' ODBC Provider in case no ACE Provider
    

提交回复
热议问题