|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 数据提取()
On Error GoTo FLGE
' 连接到数据库
Dim strCon As String
Dim cnn As New ADODB.Connection
Dim sql As String
Dim rs As New ADODB.Recordset
tStar = Timer
Filename = ThisWorkbook.FullName
' 设置连接字符串和Sheet页名称
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Filename & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=2;ReadOnly=False;CursorLocation=adUseClient"";"
cnn.Open strCon
Set Rng = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) '数据存放位置
sql = "SELECT * FROM [US GAAP TB$A:G] where [Financial Statement Item] = '1' and [Account Number] <> '';"
' Debug.Print sql
rs.Open sql, cnn, 1, 3
Rng.CopyFromRecordset rs
rs.Close
Set Rng = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) '数据存放位置
sql = "SELECT * FROM [PRC GAAP TB$A:G] where [FS Item] = '10' and [Account] <> '';"
' Debug.Print sql
rs.Open sql, cnn, 1, 3
Rng.CopyFromRecordset rs
FLGE:
Debug.Print Err.Description
' 关闭连接和对象
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Debug.Print "用时:" & Format(Timer - tStar, "0.00秒")
End Sub
|
|