|
本不想贴出较慢的方案了,一看讨论很热烈,也贴出自己较慢的方案
- Sub 简单连接()
- Dim cnn As Object, Rst As Object, SQL$, dtmtms As Date
- Dim i&, j&, x&, ar As Variant, br As Variant, cr As Variant
- Dim lngCT As Long, lngrw&, n&
- dtmtms = Timer
- br = Sheet1.[a1:k1 & ""]
- With Worksheets("结果1")
- .Activate
- .Cells.Clear
- n = 1
- End With
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes;IMEX=1';Data Source=" & ThisWorkbook.FullName
- For x = 3 To UBound(br)
- SQL = "select 班级,姓," & br(x) & " from [成绩$] order by " & br(x) & " desc,班级 "
- Set Rst = cnn.Execute(SQL)
- ar = Rst.getrows
- ReDim cr(1 To UBound(ar, 2) + 1, 1 To UBound(ar) + 2)
- lngrw = 0
- For j = 1 To UBound(cr, 2) - 1
- cr(1, j) = Rst.Fields(j - 1).Name
- Next
- cr(1, UBound(cr, 2)) = "名次"
- lngCT = 1
- For i = 2 To UBound(cr)
- If lngCT <= 10 Then
- cr(i, UBound(cr, 2)) = lngCT
- For j = 1 To UBound(cr, 2) - 1
- cr(i, j) = ar(j - 1, i - 2)
- Next
- If ar(2, i - 2) <> ar(2, i - 1) Then
- lngCT = lngCT + 1
- lngrw = i
- End If
- End If
- Next
- With Worksheets("结果1")
- .Activate
- With .Cells(1, n)
- .Value = cr(1, 3)
- .Resize(1, 4).Merge
- End With
- .Cells(2, n).Resize(1 + lngrw, UBound(cr, 2)) = cr
- With .Cells(1, n).Resize(1 + lngrw, UBound(cr, 2))
- .Borders.LineStyle = xlContinuous
- With .Font
- .Name = "微软雅黑"
- .Size = 11
- End With
- End With
- n = n + 5
- End With
- Next
- cnn.Close
- Set cnn = Nothing
- Debug.Print Format(Timer - dtmtms, "0.0000s ")
- End Sub
复制代码 |
|