|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
zxc00 发表于 2014-2-8 21:23
第10楼的代码能做成加载宏吗,如果可以的话,请也把过程详细地说说或者制作过程的视频更好,谢谢
在ThisWorkbook加菜单代码,把主程序放在模块中,另存为加载宏即可:- Private Sub Workbook_BeforeClose(Cancel As Boolean)
- On Error Resume Next
- Application.CommandBars(1).Controls("按标准提取信息").Delete
- End Sub
- Private Sub Workbook_Open()
- Dim CtrButton As CommandBarControl
- Set CtrButton = Application.CommandBars(1).Controls.Add(Type:=10, before:=11)
- With CtrButton
- .Caption = "按标准提取信息"
- With CtrButton.Controls.Add(Type:=1)
- .Caption = "按标准提取信息"
- .OnAction = "按标准提取信息"
- End With
- End With
- End Sub
复制代码 模块:- Sub 按标准提取信息()
- 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 =" & ActiveWorkbook.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
复制代码 |
评分
-
1
查看全部评分
-
|