|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
请参考:- Sub Macro1()
- '引用Microsoft AD0 Ext 2.8 for DDL and Security
- '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnn As New ADODB.Connection
- Dim Cat As New ADOX.Catalog, tb1 As Table
- Dim ok As Boolean
- Dim sh As String
- Dim s$, SQL$
- sh = "新建工作表"
- Cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';Data Source=" & ThisWorkbook.Path & "\目标工作簿.xls"
- For Each tb1 In Cat.Tables
- If tb1.Type = "TABLE" Then
- s = Replace(tb1.Name, "'", "") '表名含有数字时有多余的单引号
- If Right(s, 1) = "$" Then
- If s = sh & "$" Then
- ok = True
- Exit For
- End If
- End If
- End If
- Next
- Set Cat = Nothing
- Set tb1 = Nothing
- If ok Then
- MsgBox "不能创建工作表,目标工作簿中已经存在工作表" & sh, vbInformation
- Exit Sub
- End If
- cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.Path & "\目标工作簿.xls" '连接数据库
- SQL = "select 字段1 into [" & sh & "] from [Excel 8.0;Database=" & ThisWorkbook.Path & "\数据源.xls].[Sheet1$]"
- cnn.Execute SQL
- cnn.Close
- Set cnn = Nothing
- MsgBox "ok"
- End Sub
复制代码 |
|