I have a query where I would like to consolidate by a Code that has two rows in the initial tab, however I keep getting an error Cannot group on Fields selected with '*'. Any ideas
Option Explicit
Sub get_code()    
   OptimizeVBA True: ShDel ("Workings")    
   Dim cn As ADODB.Connection
   Set cn = New ADODB.Connection
   With cn
       .Provider = "Microsoft.ACE.OLEDB.12.0"
       .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 12.0 Macro;IMEX=1;HDR=YES"";"
       .Open
   End With
   Dim rs As ADODB.Recordset
   Set rs = New ADODB.Recordset
   rs.Open "SELECT * FROM [Holdings$] WHERE [Holdings$].['Code$'] ='%91' GROUP BY ['Code']", cn
   Dim fld As ADODB.Field
   Dim i As Integer
   Sheets.Add.name = "Workings"
   With ThisWorkbook.Worksheets("Workings")
      i = 0
      For Each fld In rs.Fields
         i = i + 1
         .Cells(1, i).Value = fld.name
       Next fld
       .Cells(2, 1).CopyFromRecordset rs
       .UsedRange.Columns.AutoFit
   End With
   rs.Close
   cn.Close
   OptimizeVBA False
End Sub
Optimize just optimizes the workbook and SHdel deletes the sheet.
 
     
    