ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: jsgj2023

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 13:50 | 显示全部楼层
Sub 提取指定班级性别科目成绩日期的数据()
Dim mydata As String
Dim mytable As String
Dim SQL As String
Dim cnn As ADODB.Connection
Dim rs  As ADODB.Recordset
Dim i   As Integer
ActiveSheet.Cells.Clear
mydata = ThisWorkbook.Path & "\成绩管理.mdb"
mytable = "考试成绩"
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .Open mydata
End With
SQL = "select * from " & mytable _
& " where (班级='初一1班' and 性别='男') and (数学>=95 or 语文>=95)" _
& " and year(日期)=2006 and month(日期)=7"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
For i = 1 To rs.Fields.Count
    Cells(1, i) = rs.Fields(i - 1).Name
Next i
Range("a2").CopyFromRecordset rs
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 15:25 | 显示全部楼层
Sub 获取指定科目成绩最高及最低分()
Dim mydata As String
Dim mytable As String
Dim sql As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
ActiveSheet.Cells.Clear
mydata = ThisWorkbook.Path & "\成绩管理.mdb"
mytable = "考试成绩"
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .Open mydata
End With
sql = "select max(数学) as math1,min(数学) as math2 from " & mytable
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
Range("a2:b1") = Array("数学最高分", "数学最低分")
Range("a2:b2") = Array(rs!math1, rs!math2)
rs.Close: cnn.Close
Set cnn = Nothing: Set rs = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 15:41 | 显示全部楼层
Sub 查询各科的平均分()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mydata As String
Dim mytable As String
Dim sql As String
Dim i As Integer
Dim classtotal  As Integer
Dim myarray  As Variant
ActiveSheet.Cells.Clear
mydata = ThisWorkbook.Path & "\成绩管理.mdb"
mytable = "考试成绩"
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .Open mydata
End With
sql = "select distinct 班级 from " & mytable
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
classtotal = rs.RecordCount
ReDim myclass(1 To classtotal)
For i = 1 To classtotal
    myclass(i) = rs.Fields("班级")
    rs.MoveNext
Next i
myarray = Array("数学", "语文", "物理", "化学", "英语", "体育", "总分")
Range("a1") = "班级"
Range("b1:h1") = myarray
For i = 1 To classtotal
    Cells(i + 1, 1) = myclass(i)
    For j = 0 To UBound(myarray)
        sql = "select avg(" & myarray(j) & ") as myavg from " & mytable _
        & " where 班级='" & myclass(i) & "'"
        Set rs = New ADODB.Recordset
        rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
        Cells(i + 1, j + 2) = Round(rs!myavg, 2)
    Next j
Next i
rs.Close: cnn.Close: Set rs = Nothing: Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 16:09 | 显示全部楼层

Sub 获取指定班级的数学平均分()
Dim mydata As String
Dim mytable As String
Dim sql As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
ActiveSheet.Cells.Clear
mydata = ThisWorkbook.Path & "\成绩管理.mdb"
mytable = "考试成绩"
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .Open mydata
End With
sql = "select *," & "(select avg(数学) as mathavg from " & mytable & " where 班级='初一2班')" & " from " _
& mytable & " where 班级='初一2班'" & " and 数学>=(select avg(数学) from " & mytable & " where 班级='初一2班')"
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
For i = 1 To rs.Fields.Count
    Cells(1, i) = rs.Fields(i - 1).Name
Next i
Range("a2").CopyFromRecordset rs
Range("a1") = "班级数学平均分"
rs.Close: cnn.Close: Set cnn = Nothing: Set rs = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-18 19:13 | 显示全部楼层
Sub 查询数学成绩大于100且总分大于620的学生()
Dim cnn  As ADODB.Connection
Dim rs  As ADODB.Recordset
Dim myworkname As String
Dim n As Integer
Dim sql As String
Dim ws  As Worksheet
Set ws = Worksheets("汇总")
ws.Cells.Clear
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .ConnectionString = "extended properties=excel 8.0;" _
    & "data source=" & ThisWorkbook.FullName
    .Open
End With
myworkname = Worksheets(2).Name
sql = "select * from [" & myworkname & "$] where 数学>100 and 总分>620"
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
n = rs.RecordCount
If n > 0 Then
    MsgBox "查询到 " & n & " 条符合条件的记录.", vbInformation
Else
    MsgBox "没有查询到符合条件的记录.", vbInformation
End If
For i = 1 To rs.Fields.Count
    ws.Cells(1, i) = rs.Fields(i - 1).Name
Next
ws.Range("a2").CopyFromRecordset rs
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing: Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-18 19:34 | 显示全部楼层

Sub 循环查找工作表中数学成绩大于100且总分大于620的学生名单()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim myworkname As String
Dim n  As Integer
Dim sql As String
Dim ws As Worksheet
Set ws = Worksheets("汇总")
ws.Cells.Clear
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .ConnectionString = "extended properties=excel 8.0;" _
    & "data source=" & ThisWorkbook.FullName
    .Open
End With
For i = 1 To Worksheets.Count
    If Worksheets(i).Name <> "汇总" Then
        myworkname = Worksheets(i).Name
        sql = "select * from [" & myworkname & "$] where 数学>100 and  总分>620"
        Set rs = New ADODB.Recordset
        rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
        For j = 1 To rs.Fields.Count
            ws.Cells(1, j) = rs.Fields(j - 1).Name
        Next
        n = ws.Range("a65536").End(3).Row
        ws.Range("a" & n + 1).CopyFromRecordset rs
    End If
Next
If ws.Range("a65536").End(3).Row > 1 Then
    MsgBox "查询到" & ws.Range("a65536").End(3).Row - 1 _
    & " 条符合条件的记录. ", vbInformation
Else
    MsgBox "没有查询到符合条件的记录. ", vbInformation
End If
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-19 00:35 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
好东西,留着以后用

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 08:57 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 指定工作簿和工作表查询指定条件学生的成绩()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim myworkname  As String
Dim n  As Integer
Dim sql As String
Dim wbname As String
Dim ws  As Worksheet
Set ws = Worksheets("汇总")
wbname = ThisWorkbook.Path & "\技巧11-001.xls"
ws.Cells.Clear
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .ConnectionString = "extended properties=excle 8.0;" _
    & "data source=" & wbname
    .Open
End With
myworkname = "初一1班"
sql = "select * from [" & myworkname & "$] where 数学>100  and 总分>620"
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
n = rs.RecordCount
If n > 0 Then
    MsgBox "查询到" & n & "条符合条件的记录.", vbInformation
Else
    MsgBox "没有查询到符合条件的记录.", vbInformation
End If
For i = 1 To rs.Fields.Count
    ws.Cells(1, i) = rs.Fields(i - 1).Name
Next
ws.Range("a2").CopyFromRecordset rs
rs.Close
cnn.Close
Set rs = Nothing: Set cnn = Nothing
Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 10:50 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 指定工作簿和工作表查询()
Dim myworkspace As DAO.Workspace
Dim cnn As DAO.Connection
Dim rs As DAO.Recordset
Dim myfilename As String
Dim cnn1 As String
Dim cnn2 As String
Dim cnn3 As String
Dim cnn4 As String
Dim sql As String
Dim i As Long
myfilename = "Adele.xls"
myworkname = "初一1班"
cnn1 = "odbc;"
cnn2 = "dbq=" & ThisWorkbook.Path & "\" & myfilename & ";"
cnn3 = "driver={microsoft excel driver (*.xls)};"
cnn4 = "filedsn=c:"
Set myworkspace = CreateWorkspace("myworkspace", "myname", "", dbUseODBC)
Workspaces.Append myworkspace
sql = "select * from [" & myworkname & "$] where 数学>100 and 总分>620"
Set cnn = myworkspace.OpenConnection( _
Name:="connection", _
Options:=dbDriverNoPrompt, _
Connect:=cnn1 & cnn2 & cnn3 & cnn4)
Set rs = cnn.OpenRecordset(sql)
With rs
    For i = 1 To .Fields.Count
        Cells(1, i).Value = .Fields(i - 1).Name
    Next i
    Range("a2").CopyFromRecordset rs
    .Close
End With
cnn.Close
myworkspace.Close
Set rs = Nothing
Set cnn = Nothing
Set myworkspace = Nothing
Set ws = Nothing
End Sub


TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 11:13 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub ADO法汇总数据()
Dim mypath  As String
Dim defaultpath As String
Dim mysql As String
Dim i As Integer
Dim j  As Integer
Dim n As Integer
Dim p  As Integer
Dim n1  As Integer
Dim n2 As Integer
Dim n3 As Integer
Dim n4 As Integer
Dim cnn As ADODB.Connection
Dim rs  As ADODB.Recordset
Worksheets("原始数据汇总").Cells.Clear
defaultpath = ThisWorkbook.Path & "\初一年级\"
mypath = InputBox("请输入要查询工作簿的文件夹完整目录及名字:" _
& vbCrLf & vbCrLf & "如果为空,则默认为" & vbCrLf _
& defaultpath, "输入路径", defaultpath)
If mypath = "" Then mypath = defaultpath
Application.StatusBar = "正在查找汇总工作簿........"
Set fs = Application.FileSearch
With fs
    .LookIn = mypath
    .FileType = msoFileTypeExcelWorkbooks
    If .Execute(SortBy:=msoSortByFileName, _
        sortorder:=msoSortOrderAscending) > 0 Then
        p = .FoundFiles.Count
        MsgBox "在此文件夹中共有" & p & "个工作表的数据文件需要汇总!", vbInformation, "搜索到汇总文件"
        ReDim myfile(p) As String
        For i = 1 To p
            myfile(i) = .foundfilea(i)
        Next i
    Else
        MsgBox "没有搜索到要汇总的文件!", vbInformation, "没有汇总文件"
        Application.StatusBar = False
        Exit Sub
    End If
End With
For i = 1 To p
    Set cnn = New ADODB.Connection
    With cnn
        .Provider = "microsoft.jet.oledb.4.0"
        .ConnectionString = "extended properties=excel 8.0;" _
        & "data source=" & myfile(i)
        .Open
    End With
    Set rs = New ADODB.Recordset
    mysql = "select * from [Sheet1$]"
    rs.Open mysql, cnn, adOpenKeyset, adLockOptimistic
    If i = 1 Then
        For j = 0 To rs.Fields.Count - 1
            Worksheets("原始数据汇总").Cells(1, j + 1) = rs.Fields(j).Name
        Next
    End If
    n = Worksheets("原始数据汇总").Range("a65536").End(3).Row
    If rs.RecordCount <> 0 Then
        Worksheets("原始数据汇总").Range("a" & n + 1).CopyFromRecordset rs
    End If
Next
Application.StatusBar = False
MsgBox "工作簿汇总完毕!共汇总了" & p & "个工作簿.", vbInformation, "汇总完毕"
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-7-5 01:58 , Processed in 0.046136 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表