这单一语句还真不行,不过以前做过一个类似的,改动了一下,还真是可以: Private Sub CommandButton1_Click() Dim SQL$, sql1$, sql2$, sql3$, i%, maxrow&, t maxrow = Sheets("出货统计").[A65536].End(xlUp).Row Set CNN = CreateObject("ADODB.Connection") CNN.Open "provider=microsoft.jet.oledb.4.0;extended properties=Excel 8.0;data source=" & ThisWorkbook.FullName For i = 1 To 12 sql1 = sql1 & "数量" & i & ",金额" & i & "," sql2 = sql2 & "(" sql3 = sql3 & " left join (select 材料编号,sum(数量) as 数量" & i & ",sum(金额) as 金额" & i & _ " from [出货统计$A2:I" & maxrow & "] where month(日期)=" & i & " group by 材料编号) as b" & i & _ " on a.材料编号=b" & i & ".材料编号)" Next sql1 = Left(sql1, Len(sql1) - 1) sql2 = Left(sql2, Len(sql2) - 1) sql3 = Left(sql3, Len(sql3) - 1) SQL = "select a.材料编号,a.型号规格," & sql1 & " from (" & sql2 & "select distinct 材料编号,型号规格 " & _ "from [出货统计$A2:I" & maxrow & "]) as a " & sql3 [a5].CopyFromRecordset CNN.Execute(SQL) CNN.Close: Set CNN = Nothing End Sub
uXvcw8mO.rar
(12.91 KB, 下载次数: 38)
|