Select Newest Record and Create New Table of Unique Values in Excel

前端 未结 2 1883
清歌不尽
清歌不尽 2020-11-30 16:14

I have an Excel table that records vehicle progress through work stations within a business. A given work station may be visited more than once.

The vehicle license

2条回答
  •  时光取名叫无心
    2020-11-30 16:36

    You mentioned SQL in your question, so I thought you might be interested in a VBA solution using SQL:

    'Assumes your data is on a sheet called "DataSheet", and you want the answers stored starting in cell A2 of a sheet called "Results")
    
    Sub test()
        Dim objConnection           As ADODB.Connection
        Dim objRecordset            As ADODB.Recordset
    
        Set objConnection = New ADODB.Connection
        Set objRecordset = New ADODB.Recordset
    
        objConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                         "Data Source=" & ThisWorkbook.FullName & ";" & _
                                         "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
    
        objConnection.Open
    
        sqlcommand = "SELECT LAST([Record ID]), " & _
                            "[Reg No], " & _
                            "LAST([Priority Level]), " & _
                            "LAST([Make]), " & _
                            "LAST([Current Stage]) " & _
                            "FROM [DataSheet$] GROUP BY [Reg No]"
        objRecordset.Open sqlcommand, objConnection, adOpenStatic, adLockOptimistic, adCmdText
        Sheets("Results").Range("A2").CopyFromRecordset objRecordset
    End Sub
    

    To use that, you will need to include References to the "Microsoft ActiveX Data Objects 6.1 Library" and "Microsoft ActiveX Data Objects Recordset 6.0 Library" in your VBA project. (At least, they're the ones I select.)

提交回复
热议问题