|
创建一个Access文件,再按照要求创建一个数据表?如果是:- Sub Macro1()
- '引用Microsoft AD0 Ext 2.x for DDL and Security
- '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim Cat As New ADOX.Catalog
- Dim MyPath As String
- Dim myTable As String
- Dim SQL As String
- Dim ok As Boolean
- MyPath = ThisWorkbook.Path & "\多条件查询.accdb"
- If Dir(MyPath) = "" Then
- Cat.Create "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyPath
- Set Cat = Nothing
- ok = True
- End If
- myTable = "结果"
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyPath
- If Not ok Then
- Set rs = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, myTable, Empty))
- If Not rs.EOF Then
- SQL = "DROP TABLE " & myTable
- cnn.Execute SQL
- End If
- End If
- SQL = "select 姓名,sum(iif(科目='数学',成绩)) as 数学,sum(iif(科目='英语',成绩)) as 英语,sum(iif(科目='语文',成绩)) as 语文 from [Excel 12.0;Database=" _
- & ThisWorkbook.FullName & ";].[基础数据$] group by 姓名"
- SQL = "select * into " & myTable & " from (" & SQL & ")"
- cnn.Execute SQL
- MsgBox "已经将查询数据生成新数据表。", vbInformation, "生成新数据表"
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|