Excel根据单元格内容提取access2003数据
时间: 2023-04-10 19:04:50 浏览: 185
ExcelQuShu.zip_Excel VBA_VBA提取_VBa_excel vba 数据库_vba excel
5星 · 资源好评率100%
可以使用ADO连接Access数据库,然后使用SQL语句查询数据,最后将查询结果导出到Excel中。以下是示例代码:
```vb
Sub ExportData()
Dim conn As Object
Dim rs As Object
Dim sql As String
Dim i As Integer
Dim j As Integer
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
'连接Access数据库
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Northwind.mdb"
'查询数据
sql = "SELECT * FROM Customers WHERE Country='USA'"
rs.Open sql, conn
'导出数据到Excel
i = 2
j = 1
Do While Not rs.EOF
Range("A" & i).Value = rs.Fields("CustomerID").Value
Range("B" & i).Value = rs.Fields("CompanyName").Value
Range("C" & i).Value = rs.Fields("ContactName").Value
Range("D" & i).Value = rs.Fields("ContactTitle").Value
Range("E" & i).Value = rs.Fields("Address").Value
Range("F" & i).Value = rs.Fields("City").Value
Range("G" & i).Value = rs.Fields("Region").Value
Range("H" & i).Value = rs.Fields("PostalCode").Value
Range("I" & i).Value = rs.Fields("Country").Value
Range("J" & i).Value = rs.Fields("Phone").Value
Range("K" & i).Value = rs.Fields("Fax").Value
i = i + 1
rs.MoveNext
Loop
'关闭记录集和连接
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub
```
阅读全文