Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1441|回复: 4

[已解决]条件排名求助

[复制链接]
发表于 2021-11-22 22:46 | 显示全部楼层 |阅读模式
在报表排名中,我需要在排名时不对“考试类型”的模拟一进行统计排名,请问需要如何修改代码:
Sub demo2()
   Set cn = CreateObject("adodb.connection")
   cn.Open "Provider=Microsoft.Ace.Oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
   SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] group by Weekly,姓名,性别,班级,科目 order by sum(分数) desc"
   [h2:m1000].ClearContents
   [h2].CopyFromRecordset cn.Execute(SQL)
   Set d = CreateObject("Scripting.Dictionary")
   a = [h1].CurrentRegion
   For i = 2 To UBound(a)
      Key = a(i, 4) & a(i, 3) & a(i, 5)
      d(Key) = d(Key) & " " & i
   Next
   b = [a1].CurrentRegion
   For i = 2 To UBound(b)
      Key = b(i, 2) & b(i, 3) & b(i, 4)
      If Key <> p Then c = 0
      c = c + 1: p = Key
      If d.exists(Key) Then
         r = Split(d(Key))(c)
         b(i, 1) = a(r, 2): b(i, 5) = a(r, 6)
      End If
   Next
   [a1].Resize(UBound(b), 5) = b
End Sub


2021-11-22_224001.png 2021-11-22_224033.png


成绩排名.zip (26.59 KB, 下载次数: 2)
发表于 2021-11-23 13:52 | 显示全部楼层
增加红色部份即可

SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] where 考试类型 <> '模拟考一' group by Weekly,姓名,性别,班级,科目 order by sum(分数) desc"

回复

使用道具 举报

 楼主| 发表于 2021-11-24 10:07 | 显示全部楼层
cutecpu 发表于 2021-11-23 13:52
增加红色部份即可

SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] where 考试类型 ...

要是我已经where语段使用了Weekly  SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$]where weekly=$weekly group by Weekly,姓名,性别,班级,科目 order by sum(分数) desc"
   Sql = Replace(Sql, "$Weekly", [A1])

这样还可以在哪里添加 where 考试类型<>"模拟考试一" 达到满足两个条件的数据排序呢
回复

使用道具 举报

发表于 2021-11-24 11:58 | 显示全部楼层    本楼为最佳答案   
531tommy 发表于 2021-11-24 10:07
要是我已经where语段使用了Weekly  SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] ...

SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] where weekly=$weekly  and 考试类型<>'模拟考试一' group by Weekly,姓名,性别,班级,科目 order by sum(分数) desc"
回复

使用道具 举报

 楼主| 发表于 2021-11-24 12:12 | 显示全部楼层
cutecpu 发表于 2021-11-24 11:58
SQL = "select Weekly,姓名,性别,班级,科目,sum(分数) from [成绩表$] where weekly=$weekly  and 考试类 ...

原来使用and,感谢了

评分

参与人数 1学分 +2 收起 理由
cutecpu + 2 不客气。祝顺心,南无阿弥陀佛!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-29 22:53 , Processed in 0.342325 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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