|
请参考:
Sub setNewTable()
Dim Cat As New ADOX.Catalog
Dim tb1 As ADOX.Table
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim MyPath As String
Dim MyName As String
Dim sh As String
Dim i As Long
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\数据库.accdb"
MyPath = ThisWorkbook.Path & "\"
MyName = Dir(MyPath & "*.xlsx")
Do While MyName <> ""
Cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyName
For Each tb1 In Cat.Tables
If tb1.Type = "TABLE" Then
sh = Replace(tb1.Name, "'", "")
If Right(sh, 1) = "$" Then
i = i + 1
myTable = Replace(sh, "$", "")
Set rs = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, myTable, Empty))
If Not rs.EOF Then cnn.Execute "DROP TABLE " & myTable
cnn.Execute "CREATE TABLE " & myTable & "(日期 date not null primary key,数量 long not null)"
cnn.Execute "INSERT INTO " & myTable & " SELECT f1 as 日期,f2 as 数量 FROM [Excel 12.0;Hdr=No;Database=" & MyPath & MyName & ";].[" & sh & "]"
End If
End If
Next
MyName = Dir
Loop
MsgBox "已成功生成" & i & "个数据表。", vbInformation
Set Cat = Nothing
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|