|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
zxc00 发表于 2014-2-3 23:30
谢谢,如果不按照标准分数(把标准分数去掉),直接提取前三名呢 - Private Sub CommandButton2_Click()
- Dim cnn As Object, rs As Object, SQL$, arr, brr&(), i&, j&, lr&, t$, objWMI As Object
- Const HKEY_LOCAL_MACHINE = &H80000002
- Set objWMI = GetObject("winmgmts:\\.\root\default:StdRegProv")
- objWMI.SetDWORDValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines\Excel", "TypeGuessRows", 100
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0;imex=1';Data Source =" & ThisWorkbook.FullName
- SQL = "select distinct left(班别,1) from [总表$] where 班别 is not null"
- arr = cnn.Execute(SQL).GetRows
- Range("a1").CurrentRegion.Offset(1).ClearContents
- For i = 0 To UBound(arr, 2)
- SQL = "select top 3 班别,姓名,iif(isnull(语文),0,语文*1)+iif(isnull(数学),0,数学*1)+iif(isnull(英语),0,英语*1) as 总分,null,left(班别,1) as 所属年级 from [总表$] where left(班别,1)='" _
- & arr(0, i) & "' order by iif(isnull(语文),0,语文*1)+iif(isnull(数学),0,数学*1)+iif(isnull(英语),0,英语*1) desc"
- Set rs = CreateObject("ADODB.Recordset")
- rs.Open SQL, cnn, 1, 3
- ReDim brr(1 To rs.RecordCount, 1 To 1)
- brr(1, 1) = 1
- t = rs.Fields(2)
- rs.MoveNext
- For j = 2 To rs.RecordCount
- If rs.Fields(2) = t Then
- brr(j, 1) = brr(j - 1, 1)
- Else
- brr(j, 1) = j
- End If
- t = rs.Fields(2)
- rs.MoveNext
- Next
- rs.MoveFirst
- lr = Range("a65536").End(xlUp).Row + 1
- Range("a" & lr).CopyFromRecordset rs
- Range("d" & lr).Resize(j - 1) = brr
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|