|
Sub 拆分文件()
Dim oCnn As Object, oRst As Object, sTabName As String, sFile As String
Dim arr, i As Long
sTabName = Sheet1.Name
Set oCnn = CreateObject("adodb.connection")
Set oRst = CreateObject("adodb.recordset")
oCnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1';data source=" & ThisWorkbook.FullName
oRst.Open "select distinct 性别 from [" & sTabName & "$]", oCnn, 1, 3
arr = oRst.getrows
For i = 0 To oRst.RecordCount - 1
sFile = ThisWorkbook.Path & "\" & arr(0, i) & ".xls"
If Dir(sFile) <> "" Then Kill sFile
oCnn.Execute ("select * into [" & sFile & "]." & arr(0, i) & " from [" & sTabName & "$] where 性别='" & arr(0, i) & "'")
Next
oRst.Close
oCnn.Close
Set oRst = Nothing
Set oCnn = Nothing
End Sub |
|