|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 将工作表生成新数据表()
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim sh As Worksheet
Dim SQL As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\模具库.accdb"
For Each sh In Worksheets
If sh.[a2] = "序号" Then
Set rs = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, sh.Name, Empty))
If Not rs.EOF Then cnn.Execute "DROP TABLE [" & sh.Name & "]"
SQL = "SELECT * FROM [Excel 12.0;Database=" & ThisWorkbook.FullName & ";].[" & sh.Name & "$" & sh.[a1].CurrentRegion.Offset(1).Address(0, 0) & "] WHERE 单号 IS NOT NULL"
SQL = "SELECT * INTO [" & sh.Name & "] FROM (" & SQL & ")"
cnn.Execute SQL
End If
Next
MsgBox "已经将工作表数据生成新数据表。", vbInformation, "生成新数据表"
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub |
评分
-
1
查看全部评分
-
|