|
楼主 |
发表于 2024-11-14 12:50
|
显示全部楼层
以下是降序排序代码,其他升序排序、并列降序、并列升序都在主贴的附件内。
- Function LARGES(Index As Variant, Array_Number As Range, Array_Text As Range, Format_Text As Variant, Optional IsTrue0 As Boolean = True) As Variant
- '降序(排名数值,数值单元格,对应文本单元格,返回格式,是否保留0值)
- '排名数值 = "第1名|第2名|第3名|..."
- Dim i As Long
- Dim ArrayLength As Long '数组长度
- Dim ArrayCount As Long '数组计数项
- Dim NumArray() As Double '数值数组
- Dim TextArray() As String '文本数组
- Dim NewNumArray() As Variant '新数值数组
- Dim NewTextArray() As Variant '新文本数组
- Dim Result() As String '输出数组
- Dim ResultValue As String '输出文本
- Dim Format_Number As String '输出数值格式
- Dim MaxValue As Double '最大值数值
- Dim MaxIndex As Long '最大值序号
- '判断传入是单元格则改为单元格内容
- If TypeName(Index) <> "String" Then Index = Index.Value
- If TypeName(Format_Text) <> "String" Then Format_Text = Format_Text.Value
- '获取范围的单元格数量
- ArrayLength = Array_Number.Cells.Count
- '确定数组大小
- ReDim NumArray(1 To ArrayLength): ReDim TextArray(1 To ArrayLength)
- ReDim NewNumArray(1 To ArrayLength): ReDim NewTextArray(1 To ArrayLength)
- '将单元格值和文本存入数组
- For i = 1 To ArrayLength
- If IsNumeric(Array_Number.Cells(i).Value) Then
- NumArray(i) = Array_Number.Cells(i).Value
- TextArray(i) = Array_Text.Cells(i).Value
- Else
- NumArray(i) = 0
- TextArray(i) = Array_Text.Cells(i).Value
- End If
- Next i
- '查找行最大值并重置数组
- ArrayCount = 1
- Do While True
- MaxValue = Application.WorksheetFunction.Max(NumArray)
- MaxIndex = Application.Match(MaxValue, NumArray, 0)
- ' 如果没有找到最大值,退出循环
- If MaxValue = -1E+308 Then
- Exit Do
- Else
- NewNumArray(ArrayCount) = NumArray(MaxIndex)
- NewTextArray(ArrayCount) = TextArray(MaxIndex)
- End If
- ' 将原 NumArray 中的该值设为 -1E+308
- NumArray(MaxIndex) = -1E+308
- ' 增加新数组的索引
- ArrayCount = ArrayCount + 1
- Loop
- Number = "Number": Format_Number = "General Number" '默认常规
- Select Case 0
- Case Is < InStr(Format_Text, "数值"): Number = "Number数值": Format_Number = "Fixed" '数值
- Case Is < InStr(Format_Text, "千分"): Number = "Number千分": Format_Number = "Standard" '千分位符号
- Case Is < InStr(Format_Text, "比例"): Number = "Number比例": Format_Number = "Percent" '百分比
- End Select
- '判断序号是否有"|",格式化数组
- If InStr(Index, "|") = 0 Then
- ResultValue = Replace(Format_Text, "Text", NewTextArray(Index), , , vbTextCompare) '替换Text,不区分大小写
- ResultValue = Replace(ResultValue, Number, Format(NewNumArray(Index), Format_Number)) '替换Number
- ResultValue = Replace(ResultValue, "Index", Index, , , vbTextCompare) '替换Index
- If IsTrue0 Then
- LARGES = ResultValue
- ElseIf IsTrue0 = False Then
- If NewNumArray(Index) = 0 Then ResultValue = ""
- LARGES = ResultValue
- End If
- Else
- Index = Split(Index, "|")
- '将排名放入格式化后的数组
- For i = LBound(Index) To UBound(Index)
- ResultValue = Replace(Format_Text, "Text", NewTextArray(Index(i)), , , vbTextCompare) '替换
- ResultValue = Replace(ResultValue, Number, Format(NewNumArray(Index(i)), Format_Number)) '替换
- ResultValue = Replace(ResultValue, "Index", Index(i), , , vbTextCompare) '替换Index
- If IsTrue0 Then
- ReDim Preserve Result(i)
- Result(i) = ResultValue
- ElseIf IsTrue0 = False And NewNumArray(i + 1) <> 0 Then
- ReDim Preserve Result(i)
- Result(i) = ResultValue
- End If
- Next i
- LARGES = Join(Filter(Result(), ""), "、")
- End If
- End Function
复制代码 |
|