如何在Excel公式中引用Access数据(透视表除外)
发布网友
发布时间:2022-04-20 12:19
我来回答
共1个回答
热心网友
时间:2023-08-04 12:31
1、连接ACCESS2007,2007以上版本代码都差不多:
Sub AC()
Dim cnn As New Connection
Dim rs As New Recordset
Dim sql As String
cnn.Open "Provider=Microsoft.Ace.OleDB.12.0;data Source=" & ThisWorkbook.Path & "\数据库.accdb"
sql = "select * from [宏站]"
rs.Open sql, cnn
'复制字段名
For i = 1 To rs.Fields.Count
Cells(1, i) = rs.Fields(i - 1).Name
Next i
[a1].CopyFromRecordset rs
rs.Close
cnn.Close
End Sub
1、连接ACCESS2003:
Dim cnn As New Connection
Dim rs As New Recordset
Dim sql As String
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;data Source=" & ThisWorkbook.Path & "\数据库.mdb"
sql = "select * from [宏站] "
rs.Open sql, cnn
'复制字段名
For i = 1 To rs.Fields.Count
Cells(1, i) = rs.Fields(i - 1).Name
Next i
'复制全部数据
Range("A2").CopyFromRecordset rs
rs.Close
cnn.Close
End Sub
追问我是access2019年的,复制您的这个到excel的宏,允许提示用户定义类型未定义 是什么意思呀