|
本帖最后由 ydot 于 2014-2-7 16:32 编辑
我在EXCEL的VBA里编写了如下代码:
- Public Sub 技巧11()
- Dim cnn As ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim myWorkName, workname2 As String, n As Integer, Sql As String
- Dim ws As Worksheet
- Set ws = Worksheets("汇总")
- ws.Cells.Clear
- '建立与当前工作簿的连接
- Set cnn = New ADODB.Connection
- With cnn
- .Provider = "microsoft.jet.oledb.4.0"
- .ConnectionString = "Extended Properties=Excel 8.0;" _
- & "Data Source=" & ThisWorkbook.FullName
- .Open
- End With
- Sql = "SELECT [排名$].排名, t.NSRSBH, t.NSRMC, sum(增值税2012_1) as 增2012_1 FROM (SELECT NSRSBH, NSRMC, IIf(year(RKRQ)=2012 AND month(RKRQ)=1,sum(SE),0) AS 增值税2012_1 FROM [入库$] WHERE ZSXM_DM='增值税' GROUP BY NSRSBH, NSRMC,RKRQ) AS t ,[排名$] where t.NSRSBH=[排名$].NSRSBH GROUP BY t.NSRSBH, t.NSRMC, [排名$].排名 ORDER BY [排名$].排名"
- Set rs = New ADODB.Recordset
- rs.Open Sql, cnn, adOpenKeyset, adLockOptimistic
- n = rs.RecordCount
- If n > 0 Then
- MsgBox "查询到 " & n & " 条符合条件的记录。", vbInformation
- Else
- MsgBox "没有查询到符合条件的记录。", vbInformation
- End If
- '复制标题
- For i = 1 To rs.Fields.Count
- ws.Cells(1, i) = rs.Fields(i - 1).Name
- Next i
- '复制查询到的记录
- ws.Range("A2").CopyFromRecordset rs
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set ws = Nothing
- End Sub
复制代码
运行时显示“运行时错误‘-2147217913’(80040e07) 标准表达式中数据类型不匹配 ”,要是删了SQL语句中的“ sum(增值税2012_1) as 增2012_1”,就能运行了,请问这句我该如何写
|
|