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-16 16:28 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 将重复的数据标示为红色()
Dim myrange As Range
Set myrange = ActiveSheet.Range("a1:a10")
With myrange
    .ClearFormats
    .FormatConditions.AddUniqueValues
    .FormatConditions(1).DupeUnique = xlDuplicate
    With .FormatConditions(1).Font
        .Bold = True
        .Color = vbRed
    End With
End With
Set myrange = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-16 16:44 | 显示全部楼层
Sub 标识不重复的数据为红色()
Dim myrange As Range
Set myrange = ActiveSheet.Range("a1:a10")
With myrange
    .ClearFormats
    .FormatConditions.AddUniqueValues
    .FormatConditions(1).DupeUnique = xlUnique
    With .FormatConditions(1).Font
        .Bold = True
        .Color = vbRed
    End With
End With
Set myrange = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 09:21 | 显示全部楼层
Sub 判断指定的数据库是否存在()
Dim cnn As ADODB.Connection
Dim cnnstr   As String
Dim mydata   As String
mydata = "商品信息"
Set cnn = New ADODB.Connection
cnnstr = "provider=sqloledb.1;" _
& "user id=sa;" _
& "data  source=THTFCOMPUTER;" _
& "Initial Catalog=" & mydata
cnn.ConnectionString = cnnstr
On Error Resume Next
cnn.Open
On Error GoTo 0
If cnn.State = adStateOpen Then
    MsgBox "数据库<" & mydata & ">存在"
    cnn.Close
Else
    MsgBox "数据库<" & mydata & ">不存在"
End If
Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 10:17 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
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 distinct 班级 from " & mytable
Set rs = New ADODB.Recordset
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
Range("a1").CopyFromRecordset rs
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 10:33 | 显示全部楼层
Sub 提取数学成绩大于等于90的前十名_按降序排列()
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
mydata = ThisWorkbook.Path & "\成绩管理.mdb"
mytable = "考试成绩"
Set cnn = New ADODB.Connection
With cnn
    .Provider = "microsoft.jet.oledb.4.0"
    .Open mydata
End With
sql = "select top 10 * from " & mytable & " where 数学>=90  order by 数学 desc"
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
Range("a2").CopyFromRecordset rs
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 10: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
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  姓名 like '郭%'"
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
Range("a2").CopyFromRecordset rs
rs.Close: cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-17 11:01 | 显示全部楼层
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 _
& " order by 总分 desc,数学 desc,语文 desc"
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 11:19 | 显示全部楼层
Sub 统计初一班数学成绩在80至90之间的学生名单()
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 数学 between 80 and 90"
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("a12").CopyFromRecordset rs
rs.Close
cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub

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
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

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

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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