Sub 比对两次成绩()
CreateAdvance "进退比较", "月考2", "期中考", "月考2", "月考3"
End Sub
Sub CreateAdvance(ByVal MainName As String, ByVal ShtName1 As String, ByVal ShtName2 As String _
, ByVal ExamName1 As String, ByVal ExamName2 As String)
Dim Ar, Br
Dim sht As Worksheet
Dim Arr() As Variant
Dim dNo As Object
Dim dRank As Object
Dim dRow As Object
Dim OneKey
Dim Key As String Const START_COL As Long = 4
Set sht = ThisWorkbook.Worksheets(MainName)
Set dNo = CreateObject("Scripting.Dictionary")
Set dRank = CreateObject("Scripting.Dictionary")
Set dRow = CreateObject("Scripting.Dictionary")
'获取成绩数组
Ar = GetArray(ShtName1, 0, "A", "S")
Br = GetArray(ShtName2, 0, "A", "S")
'
For i = LBound(Ar) + 1 To UBound(Ar) Step 1 Key = CStr(Ar(i, 1))
dNo(Key) = Array(Ar(i, 1), Ar(i, 2), Ar(i, 3)) '储存号 名 班 信息
For J = LBound(Ar, 2) To UBound(Ar, 2)
K = Key & ExamName1 & Ar(1, J) '创建关键字 学号 & 考试名称 & 科目/排名
'Debug.Print K
dRank(K) = Ar(i, J) '储存所有信息
Next J
Next i
For i = LBound(Br) + 1 To UBound(Ar) Step 1
Key = CStr(Br(i, 1))
dNo(Key) = Array(Br(i, 1), Br(i, 2), Br(i, 3)) '储存号 名 班 信息
For J = LBound(Br, 2) To UBound(Br, 2)
K = Key & ExamName2 & Br(1, J) '创建关键字 学号 & 考试名称 & 科目/排名
'Debug.Print K
dRank(K) = Br(i, J) '储存所有信息
Next J
Next i '重定义合并成绩表数组 行数为学生人数+标题1行 列数为每科4列 只保留排名列所以/2
ReDim Arr(1 To dNo.Count + 1, 1 To (UBound(Ar, 2) - START_COL + 1) / 2 * 4 + START_COL - 1)
'Debug.Print UBound(Arr, 2)
For J = 1 To START_COL - 1
Arr(1, J) = Ar(1, J)
Next J
'编制新表头
x = 0
For J = START_COL To UBound(Ar, 2)
If Ar(1, J) Like "*排*" Then
x = x + 1
y = (START_COL - 1) + (x - 1) * 4 + 1
Arr(1, y) = ExamName1 & Ar(1, J)
Arr(1, y + 1) = ExamName2 & Ar(1, J)
Arr(1, y + 2) = Ar(1, J) & "进退幅度"
Arr(1, y + 3) = Ar(1, J) & "进退排名"
End If
Next J '将字典中的学生信息赋值给数组
i = 1
For Each OneKey In dNo.Keys
i = i + 1
Ar = dNo(OneKey)
Arr(i, 1) = CStr(Ar(0))
Arr(i, 2) = Ar(1)
Arr(i, 3) = Ar(2)
For J = START_COL To UBound(Arr, 2)
If Arr(1, J) Like "*排" Then
Key = CStr(Arr(i, 1)) & Arr(1, J)
'Debug.Print Key
Arr(i, J) = dRank(Key)
ElseIf Arr(1, J) Like "*幅度" Then
Arr(i, J) = Val(Arr(i, J - 2)) - Val(Arr(i, J - 1))
End If
Next J
Next OneKey '分班分科插入进退步幅的排名公式
With sht
.Cells.Clear
Set Rng = .Cells(1, 1)
Set Rng = Rng.Resize(UBound(Arr), UBound(Arr, 2))
Rng.Value = Arr
Sort_2003 Rng, True, True, 3
Arr = Rng.Value
For i = LBound(Arr) + 1 To UBound(Arr)
Key = CStr(Arr(i, 3))
If Not dRow.Exists(Key) Then
Ar = Array(i, 0)
dRow(Key) = Ar
Else
Ar = dRow(Key)
Ar(1) = i
dRow(Key) = Ar
End If
Next i For J = 1 To UBound(Arr, 2)
If Arr(1, J) Like "*排名" Then
For Each OneKey In dRow.Keys
Ar = dRow(OneKey)
StartRow = Ar(0)
EndRow = Ar(1)
Set OneRng = .Range(.Cells(StartRow, J), .Cells(EndRow, J))
AddRankFormula OneRng, StartRow, EndRow
Next OneKey
End If
Next J '复制粘贴替换公式
Arr = Rng.Value
Rng.Value = Arr
'格式调整
Rng.Columns.AutoFit
SetBorders Rng
SetCenters Rng
End With Set dNo = Nothing
Set dRank = Nothing
Set sht = Nothing
Set Rng = Nothing End Sub
Public Function GetArray(ByVal SheetName As String, ByVal HeadRow As Long, ByVal StartCol As String, ByVal EndCol As String) As Variant
Dim sht As Worksheet
Dim Rng As Range
Dim Arr As Variant
Set sht = ThisWorkbook.Worksheets(SheetName)
With sht
EndRow = .Cells(.Cells.Rows.Count, StartCol).End(xlUp).Row
Set Rng = .Range(.Cells(HeadRow + 1, StartCol), .Cells(EndRow, EndCol))
Arr = Rng.Value
GetArray = Arr
End With
Set Rng = Nothing
Set sht = Nothing
Erase Arr
End Function
Public Sub Sort_2003(ByVal Rng As Range, Optional WithHeader As Boolean = True, Optional OrderByAscending As Boolean = True, Optional SortColumnNo As Long = 1)
With Rng
.Sort _
Key1:=Rng.Cells(1, SortColumnNo), Order1:=IIf(OrderByAscending, xlAscending, xlDescending), _
Header:=IIf(WithHeader, xlYes, xlNo), MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
End With
End Sub
Sub AddRankFormula(ByVal Rng As Range, ByVal StartRow As Long, ByVal EndRow As Long)
Rng.FormulaR1C1 = "=RANK(RC[-1],R" & StartRow & "C[-1]:R" & EndRow & "C[-1])"
End Sub
Public Sub SetBorders(ByVal Rng As Range)
With Rng.Borders
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
Public Sub SetCenters(ByVal Rng As Range)
With Rng
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End Sub

  

20171104xlVBA进退比较的更多相关文章

  1. 20171104xlVBA各人各科进退

    Sub 各班个人各科进步幅度() Dim dRank As Object Set dRank = CreateObject("Scripting.Dictionary") Dim ...

  2. 第三十 访问财富进退自如 —Spring交易管理

    6月16日本,明确. "应该留给追穷寇勇,不可沽名学霸王.天若有情天亦老,人间正道是沧桑." 有始有终.有往有还.进退自如乃Spring事务管理之道,也是万物生生不息.和谐共处之道 ...

  3. 20171104xlVBA制作联合成绩条

    Dim dGoal As Object Dim dCls As Object Sub 制作联合成绩条() Dim sht As Worksheet Dim HeadRng As Range Dim H ...

  4. SQL Server2016升级前几点自检

    SQL Server2016已经出来一段时间了,而且最新的SP1包也于2016年11月18日正式发布,各种新的特性推出让我们跃跃欲试.那么对于我们真实的业务环境,特别是生产环境要不要"跟风& ...

  5. XSS 前端防火墙 —— 整装待发

    到目前为止,我们把能用前端脚本防御 XSS 的方案都列举了一遍. 尽管看起来似乎很复杂累赘,不过那些是理论探讨而已,在实际中未必要都实现.我们的目标只是为了预警,能发现问题就行,并非要做到滴水不漏的程 ...

  6. 分布式系统理论基础 - 一致性、2PC和3PC

    引言 狭义的分布式系统指由网络连接的计算机系统,每个节点独立地承担计算或存储任务,节点间通过网络协同工作.广义的分布式系统是一个相对的概念,正如Leslie Lamport所说[1]: What is ...

  7. 小程序和APP谁将主导未来?

    APP和小程序的未来会怎么样?小程序的出现真的会加速APP的灭亡吗?今天这篇文章,是对小程序和App未来发展格局的一些思考,更多的是想提醒各位拥抱小程序的的参与者,我们在决定参与这场狂欢的同时,切勿盲 ...

  8. #研发解决方案#分布式并行计算调度和管理系统Summoner

    郑昀 创建于2015/11/10 最后更新于2015/11/12 关键词:佣金计算.定时任务.数据抽取.数据清洗.数据计算.Java.Redis.MySQL.Zookeeper.azkaban2.oo ...

  9. 优化MySchool数据库设计总结

    数据库的设计   一:什么是数据库设计? 数据库设计就是将数据库中的数据实体以及这些数据实体之间的关系,进行规范和结构化的过程. 二:为什么要实施数据库设计? 1:良好的数据库设计可以有效的解决数据冗 ...

随机推荐

  1. 单用户模式破解root密码

  2. topcoder srm 350 div1

    problem1 link 满足$a^{b}\leq5000000,b>1$的数字不多,有2000多个,直接暴力计算能组成哪些数字即可. import java.util.*; import j ...

  3. 再谈 linux 的sed用法

    很多东西, 如果太复杂, 太庞杂, 一开始, 可以只掌握最简单的. 如果连最简单的都不能掌握, 那看那么多有什么用? 关于cut, sed的 处理过程和思想? vm: virtual machine, ...

  4. Nvme固体硬盘Intel750,SM961分别使用一段时间以后对比

    在SM961使用了一年半(2017年1月17日购买)后,再次测试,这次测试使用AS_SSD_Benchmark工具进行测试 感觉CrystalDiskMark工具测出来的分数在所以工具中分数最高 看图 ...

  5. 解决Visual Studio(2017)软件无法重新生成问题

    https://blog.csdn.net/qq_38265674/article/details/80539228 笔者用VS2017打开VS2015创建的工程,出现如下图的问题. 不小心没有升级平 ...

  6. 【做题】51NOD1753 相似子串——哈希

    题意:两个字符串相似定义为: 1.两个字符串长度相等 2.两个字符串对应位置上至多有一个位置所对应的字符不相同 给定一个字符串\(s\),\(T\)次询问两个子串在给定的规则下是否相似.给定的规则指每 ...

  7. vue的全局方法和局部方法

    var infiniteScroll = require('vue-infinite-scroll') 等价写法 import infiniteScroll from 'vue-infinite-sc ...

  8. Lintcode241-String to Integer - Naive

    Given a string, convert it to an integer. You may assume the string is a valid integer number that c ...

  9. Spring项目JUnit测试报错ClassNotFoundException解决

    Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...

  10. codeforces gym 101164 K Cutting 字符串hash

    题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...