1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

帖子
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 239|回复: 10

[求助] 日期范围内出现最多姓名及第二多

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-4-6 23:34 | 显示全部楼层 |阅读模式


C8开始日期、D8结束日期、d列名字范围、c列日期范围




=MOR(C11:C28,C8,C11:C28,D8,D11:D28,1)


你的帮助不胜激

Forum .zip

15.21 KB, 下载次数: 15

TA的精华主题

TA的得分主题

发表于 2025-4-7 00:52 | 显示全部楼层
代码有什么问题?

TA的精华主题

TA的得分主题

发表于 2025-4-7 07:05 | 显示全部楼层
现在的自定义函数是可以实现你需要的效果的呀,

TA的精华主题

TA的得分主题

发表于 2025-4-7 07:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
看出来了,最小的第一名,不是最多的第一名。

TA的精华主题

TA的得分主题

发表于 2025-4-7 07:18 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
不想改AI写的代码,新写一个,供参考。。。

Forum .zip

21.04 KB, 下载次数: 13

TA的精华主题

TA的得分主题

发表于 2025-4-7 07:19 | 显示全部楼层
  1. Sub ykcbf()    '//2025.4.7
  2.     Application.ScreenUpdating = False
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     With ActiveSheet
  5.         r = .Cells(Rows.Count, 3).End(xlUp).Row
  6.         arr = .Range("c10:d" & r)
  7.         rq1 = .[c8].Value
  8.         rq2 = .[d8].Value
  9.         For i = 2 To UBound(arr)
  10.             If arr(i, 1) >= rq1 And arr(i, 1) <= rq2 Then
  11.                 s = Trim(arr(i, 2))
  12.                 d(s) = d(s) + 1
  13.             End If
  14.         Next i
  15.         ReDim brr(1 To d.Count, 1 To 2)
  16.         For Each k In d.Keys
  17.             m = m + 1
  18.             brr(m, 1) = k
  19.             brr(m, 2) = d(k)
  20.         Next k
  21.         For i = LBound(brr, 1) To UBound(brr, 1)
  22.             For j = i + 1 To UBound(brr, 1)
  23.                 If brr(j, 2) > brr(i, 2) Then
  24.                     temp = brr(i, 1)
  25.                     brr(i, 1) = brr(j, 1)
  26.                     brr(j, 1) = temp
  27.                     temp = brr(i, 2)
  28.                     brr(i, 2) = brr(j, 2)
  29.                     brr(j, 2) = temp
  30.                 End If
  31.             Next j
  32.         Next i
  33.         top1 = brr(1, 1)
  34.         count1 = brr(1, 2)
  35.         If UBound(brr, 2) >= 1 Then
  36.             hasSecond = True
  37.             top2 = brr(2, 1)
  38.             count2 = brr(2, 2)
  39.         Else
  40.             hasSecond = False
  41.         End If
  42.         .[f9] = top1
  43.         .[h9] = top2
  44.     End With
  45.     Set d = Nothing
  46.     Application.ScreenUpdating = True
  47.     MsgBox "OK!"
  48. End Sub
复制代码


评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2025-4-7 09:33 | 显示全部楼层
借楼学习一下字典
Sub test()
arr = [c10].CurrentRegion
Set dic = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
   If arr(i, 1) >= [c8] And arr(i, 1) <= [d8] Then
      dic(arr(i, 2)) = dic(arr(i, 2)) + 1
   End If
Next
ReDim brr(1 To dic.Count, 1 To 2)
For Each kk In dic.keys
   m = m + 1
   brr(m, 1) = kk
   brr(m, 2) = dic(kk)
Next
For i = 1 To UBound(brr) - 1
   For j = i + 1 To UBound(brr)
      If brr(i, 2) < brr(j, 2) Then
         For k = 1 To UBound(brr, 2)
            temp = brr(j, k): brr(j, k) = brr(i, k): brr(i, k) = temp
         Next
      End If
   Next
Next
[f9] = brr(1, 1)
[h9] = brr(2, 1)
Set dic = Nothing
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2025-4-8 10:09 | 显示全部楼层
本帖最后由 taller 于 2025-4-8 10:25 编辑
  1. ' 不使用字典排序,添加字典元素时可以记录最大值
  2. ' 删除最大值之后,可以查找次大值

  3. Function MOR(DateRange1 As Range, StartDate As Date, DateRange2 As Range, EndDate As Date, NameRange As Range, Rank As Integer) As Variant
  4.     If DateRange1.Cells.Count <> DateRange2.Cells.Count Or DateRange1.Cells.Count <> NameRange.Cells.Count Then
  5.         MOR = CVErr(xlErrValue)
  6.         Exit Function
  7.     End If
  8.     Dim freqDict As Object
  9.     Set freqDict = CreateObject("Scripting.Dictionary")
  10.     Dim i As Long, iMax As Long, sMaxName As String
  11.     For i = 1 To DateRange1.Cells.Count
  12.         Dim currentDate1 As Variant
  13.         currentDate1 = DateRange1.Cells(i).Value
  14.         Dim currentDate2 As Variant
  15.         currentDate2 = DateRange2.Cells(i).Value
  16.         Dim currentName As Variant
  17.         currentName = NameRange.Cells(i).Value
  18.         If IsDate(currentDate1) And IsDate(currentDate2) Then
  19.             Dim d1 As Date
  20.             d1 = CDate(currentDate1)
  21.             Dim d2 As Date
  22.             d2 = CDate(currentDate2)
  23.             If d1 >= StartDate And d2 <= EndDate Then
  24.                 If freqDict.Exists(currentName) Then
  25.                     freqDict(currentName) = freqDict(currentName) + 1
  26.                     If freqDict(currentName) > iMax Then
  27.                         iMax = freqDict(currentName)
  28.                         sMaxName = currentName
  29.                     End If
  30.                 Else
  31.                     freqDict.Add currentName, 1
  32.                 End If
  33.             End If
  34.         End If
  35.     Next i
  36.     If freqDict.Count = 0 Then
  37.         MOR = CVErr(xlErrNA)
  38.         Exit Function
  39.     End If
  40.     MOR = ""
  41.     If rank = 1 Then
  42.         MOR = sMaxName
  43.     Elseif rank = 2 Then
  44.         freqDict.Remove sMaxName
  45.         Dim counts: counts = freqDict.Items
  46.         iMax = Application.Max(counts)
  47.         Dim vKey
  48.         For Each vKey In freqDict.Keys
  49.             If freqDict(vKey) = iMax Then
  50.                 sMaxName = vKey
  51.                 Exit For
  52.             End If
  53.         Next vKey        
  54.         MOR = sMaxName
  55.     End If
  56. End Function
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-4-10 09:04 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2025-4-10 11:22 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

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

GMT+8, 2025-4-16 05:14 , Processed in 0.025448 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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