|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 学习字典_复习ADO_1楼附件代码参考()
Dim cnn As Object, rs As Object, i&, SQL$, sh As Worksheet, rng As Range
Application.DisplayAlerts = False
For Each sh In Sheets
If sh.Name <> "全体名单" Then sh.Delete
Next
Application.DisplayAlerts = True
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties =Excel 12.0;Data Source =" & ThisWorkbook.FullName
SQL = "select 姓名 as 姓名,性别 AS 性别,语文 AS 语文,数学 AS 数学,英语 AS 英语,总分 AS 总分,考号*1 AS 考号,班级*1 AS 班级 from [全体名单$] ORDER BY 班级*1,考号*1 "
SQL = "select distinct 班级 from (" & SQL & ") "
Set rst = cnn.Execute(SQL)
Do Until rst.EOF
SQL = "select 姓名,性别,语文,数学,英语,总分,考号,班级 from [全体名单$] where 班级='" & rst.Fields(0).Value & "'"
Set rs = cnn.Execute(SQL)
With Sheets.Add(After:=Sheets(Sheets.Count))
.Name = rst.Fields(0).Value
i = 0
For Each Field In rs.Fields
aa = Field.Name
[A1].Offset(0, i) = aa
i = i + 1
Next
.[a2].CopyFromRecordset cnn.Execute(SQL)
End With
rst.MoveNext
Loop
rs.Close
rst.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub |
|