zxc00 发表于 2014-2-2 21:50
红色字的要求是,没有达到标准分数的就提取该年级的前三名。 - 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
- arr = Range("G11:H" & Range("G65536").End(xlUp).Row)
- Range("a1").CurrentRegion.Offset(1).ClearContents
- For i = 1 To UBound(arr)
- SQL = "select 班别,姓名,iif(isnull(语文),0,语文*1)+iif(isnull(数学),0,数学*1)+iif(isnull(英语),0,英语*1) as 总分,null,left(班别,1) as 所属年级 from [总表$] where left(班别,1)='" _
- & arr(i, 1) & "' and iif(isnull(语文),0,语文*1)+iif(isnull(数学),0,数学*1)+iif(isnull(英语),0,英语*1)" & arr(i, 2) & " 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
- If rs.RecordCount = 0 Then
- 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(i, 1) & "' 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
- End If
- 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
复制代码 |