|
本帖最后由 VinceDalen 于 2018-8-17 17:00 编辑
- Sub 分组求和()
- Dim cnn As Object, rst As Object
- Dim mypath As String
- Dim str_cnn As String
- Dim sql As String
- Dim i As Long
- Set cnn = CreateObject("adodb.connection")
- mypath = ThisWorkbook.FullName
- If Application.Version < 12 Then
- str_cnn = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & mypath
- Else
- str_cnn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & mypath
- End If
- cnn.Open str_cnn
- <div>sql = "SELECT 银行账号,SUM(交易金额) AS 2018年1月交易量 FROM [Sheet2$] GROUP BY 银行账号"</div><div>Set rst = cnn.Execute(sql)
- </div> Worksheets(Sheet3).[a:b].ClearContents
- For i = 0 To rst.Fields.Count - 1
- Worksheets(Sheet3).Cells(1, i + 1) = rst.Fields(i).Name
- Next
- Worksheets(Sheet3).Range("a2").CopyFromRecordset rst
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 Set rst = cnn.Execute(sql)这句报错
|
|