|
本帖最后由 zhaogang1960 于 2012-6-7 23:09 编辑
请测试- Sub Macro1()
- Dim cnn As Object, rs As Object, SQL$, sFile$, i&
- Set cnn = CreateObject("adodb.connection")
- Set rs = CreateObject("adodb.Recordset")
- cnn.Open "Provider=Microsoft.ace.Oledb.12.0;Extended Properties=Excel 12.0 Xml;Data Source=" & ThisWorkbook.FullName
- SQL = "select distinct 部门 from [" & ActiveSheet.Name & "$] where 部门 is not null"
- rs.Open SQL, cnn, 1, 3
- For i = 1 To rs.RecordCount
- sFile = ThisWorkbook.Path & "" & rs.Fields(0).Value & ".xlsx"
- If Dir(sFile) <> "" Then Kill sFile
- SQL = "select * into [" & sFile & "].[Sheet1] from [" & ActiveSheet.Name & "$] where 部门='" & rs.Fields(0).Value & "'"
- cnn.Execute SQL
- rs.MoveNext
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- MsgBox "拆分完成!", vbInformation
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|