|
Sub A()
Dim cnn, rs As Object, Sql As String, sh As Worksheet, arr, i%, M
Set cnn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.Recordset")
arr = Sheet1.Range("d2:d" & Sheet1.[d99].End(3).Row)
Application.DisplayAlerts = False
For Each sh In Sheets
If sh.Name <> "Sheet1" Then sh.Delete
Next
Application.DisplayAlerts = True
M = (Sheet1.[a999].End(3).Row - 1) \ UBound(arr)
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0;Extended Properties='Excel 12.0;HDR=NO'; Data Source=" & ThisWorkbook.FullName
Sql = "select * from [sheet1$A2:C" & [A9999].End(3).Row & "]"
rs.Open Sql, cnn, 1, 1
For i = 1 To UBound(arr)
Sheets.Add after:=Sheets(Sheets.Count)
With ActiveSheet
.Name = arr(i, 1)
.[b1] = arr(i, 1)
If i = UBound(arr) Then
.[a2].CopyFromRecordset rs
Else
.[a2].CopyFromRecordset rs, M
End If
.Columns("a:c").EntireColumn.AutoFit
End With
Next
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|