ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 请字典+数组的高手请进

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2012-4-18 10:55 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
cxh2010 发表于 2012-4-17 22:17
大哥,当某班的某位学生的所有科目的成绩都为空时,代码运行出现了问题,请抽空看看,谢谢

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) <> "A2" Then Exit Sub
    Range("A4:O37").ClearContents
    If Target = "" Then Exit Sub
    Dim c As Range, arr, i&, ban$, brr(100, 1 To 2), n&
    Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
    ban = Sheets("班档").Range("a2").Value
    With Sheets("成绩总表")
        Set c = .[a:a].Find(ban, , , xlWhole)
        If c Is Nothing Then
            MsgBox "没有查到"
            Exit Sub
        End If
        arr = c.Resize(.[a:a].Find(ban, , , xlWhole, , xlPrevious).Row - c.Row + 1, 5)
    End With
    n = 1
    brr(1, 2) = 0
    For i = 1 To UBound(arr)
        hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
        For j = 1 To n
            If hj >= brr(j, 2) Then
                For m = n To j Step -1
                    brr(m + 1, 1) = brr(m, 1)
                    brr(m + 1, 2) = brr(m, 2)
                Next
                brr(j, 1) = i
                brr(j, 2) = hj
                Exit For
            End If
        Next
        n = n + 1
    Next
    For i = 1 To n - 1
        If i > 34 Then
            x = i - 34
            y = 8
        Else
            x = i
            y = 0
        End If
        If brr(i, 2) <> brr(i - 1, 2) Then mc = i
        crr(x, 1 + y) = mc
        For j = 2 To 5
            crr(x, j + y) = arr(brr(i, 1), j)
        Next
        crr(x, 6 + y) = brr(i, 2)
    Next
    Sheets("班档").Range("a4:o37") = crr
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2013-2-14 11:07 | 显示全部楼层
zhaogang1960 发表于 2012-4-17 23:31

老师,新年好,这是一个双条件查询的求助,请进

查询.zip

52.87 KB, 下载次数: 10

TA的精华主题

TA的得分主题

发表于 2013-2-14 13:37 | 显示全部楼层
tplk 发表于 2013-2-14 11:07
老师,新年好,这是一个双条件查询的求助,请进

太久了,已经全忘了,先请测试:
  1. Dim dicValidation As Object

  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3.     If Target.Address(0, 0) <> "G1" And Target.Address(0, 0) <> "B3" Then Exit Sub
  4.     Dim arr, i&, s$
  5.     With Sheets("学生成绩源")
  6.         arr = .Range("A2:C" & .Range("A65536").End(xlUp).Row)
  7.     End With
  8.     If dicValidation Is Nothing Then Set dicValidation = CreateObject("scripting.dictionary")
  9.     For i = 1 To UBound(arr)
  10.         If Not dicValidation.Exists(arr(i, 1)) Then Set dicValidation(arr(i, 1)) = CreateObject("scripting.dictionary")
  11.         dicValidation(arr(i, 1))(arr(i, 3)) = ""
  12.     Next
  13.     With [g1].Validation
  14.         .Delete
  15.         .Add 3, 1, 1, Join(dicValidation.keys, ",")
  16.     End With
  17. End Sub

  18. Private Sub Worksheet_Change(ByVal Target As Range)
  19.     If Target.Address(0, 0) <> "B3" And Target.Address(0, 0) <> "G1" Then Exit Sub
  20.     If [g1] = "" Or [b3] = "" Then Exit Sub
  21.     If Target.Address(0, 0) = "G1" Then
  22.         With [b3].Validation
  23.             .Delete
  24.             If dicValidation.Exists(Target.Value) Then .Add 3, 1, 1, Join(dicValidation(Target.Value).keys, ",")
  25.         End With
  26.         [b3] = ""
  27.         Exit Sub
  28.     End If
  29.     Range("A5:O38").ClearContents
  30.     Dim c As Range, c2 As Range, arr, i&, 校$, 班$, brr(100, 1 To 2), n&, rng As Range
  31.     Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
  32.     校 = Range("g1").Value
  33.     班 = Range("b3").Value
  34.     With Sheets("学生成绩源")
  35.         Set c = .[a:a].Find(校, , , xlWhole)
  36.         If c Is Nothing Then
  37.             MsgBox 校 & "没有查到"
  38.             Exit Sub
  39.         End If
  40.         Set rng = c.Offset(, 2).Resize(.[a:a].Find(校, , , xlWhole, , xlPrevious).Row - c.Row + 1)
  41.         Set c2 = rng.Find(班, , , xlWhole)
  42.         If c2 Is Nothing Then
  43.             MsgBox 班 & "没有查到"
  44.             Exit Sub
  45.         End If
  46.         arr = c2.Resize(rng.Find(班, , , xlWhole, , xlPrevious).Row - c2.Row + 1, 5)
  47.     End With
  48.     n = 1
  49.     brr(1, 2) = 0
  50.     For i = 1 To UBound(arr)
  51.         hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
  52.         For j = 1 To n
  53.             If hj >= brr(j, 2) Then
  54.                 For m = n To j Step -1
  55.                     brr(m + 1, 1) = brr(m, 1)
  56.                     brr(m + 1, 2) = brr(m, 2)
  57.                 Next
  58.                 brr(j, 1) = i
  59.                 brr(j, 2) = hj
  60.                 Exit For
  61.             End If
  62.         Next
  63.         n = n + 1
  64.     Next
  65.     For i = 1 To n - 1
  66.         If i > 34 Then
  67.             x = i - 34
  68.             y = 8
  69.         Else
  70.             x = i
  71.             y = 0
  72.         End If
  73.         If brr(i, 2) <> brr(i - 1, 2) Then mc = i
  74.         crr(x, 1 + y) = mc
  75.         For j = 2 To 5
  76.             crr(x, j + y) = arr(brr(i, 1), j)
  77.         Next
  78.         crr(x, 6 + y) = brr(i, 2)
  79.     Next
  80.     Range("a5:o38") = crr
  81. End Sub

复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2013-2-14 13:38 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
请看附件
查询.rar (46.3 KB, 下载次数: 19)

TA的精华主题

TA的得分主题

发表于 2013-2-14 13:59 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 tplk 于 2013-2-14 14:00 编辑
zhaogang1960 发表于 2013-2-14 13:38
请看附件


老师,还有点小问题:
      当G1选“光明”,B3出现的有效性正确,又点击G1选“老朋”,B3出现的有效性不对阿(多了六(2))。
      如果连续点击G1选“老朋”,B3出现的有效性正确,又点击G1选“光明”,B3出现的有效性不对阿(少了一个六(2)。

请老师说说这个问题是什么原因的

TA的精华主题

TA的得分主题

发表于 2013-2-14 14:15 | 显示全部楼层
zhaogang1960 发表于 2013-2-14 13:38
请看附件

老师,我下载了这个二级数据有效性,第二级的下拉列表更新快,但我不会借鉴,您看看,好吗

建立二级数据有效性及查询汇总2(2).rar

14.6 KB, 下载次数: 7

TA的精华主题

TA的得分主题

发表于 2013-2-14 14:20 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
tplk 发表于 2013-2-14 13:59
老师,还有点小问题:
      当G1选“光明”,B3出现的有效性正确,又点击G1选“老朋”,B3出现的有效 ...
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     If Target.Address(0, 0) <> "B3" And Target.Address(0, 0) <> "G1" Then Exit Sub
  3.     If Target.Address(0, 0) = "G1" Then
  4.         With [b3].Validation
  5.             .Delete
  6.             If dicValidation.Exists(Target.Value) Then .Add 3, 1, 1, Join(dicValidation(Target.Value).keys, ",")
  7.         End With
  8.         [b3] = ""
  9.         Exit Sub
  10.     End If
  11.     Range("A5:O38").ClearContents
  12.     If [b3] = "" Then Exit Sub
  13.     Dim c As Range, c2 As Range, arr, i&, 校$, 班$, brr(100, 1 To 2), n&, rng As Range
  14.     Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
  15.     校 = Range("g1").Value
  16.     班 = Range("b3").Value
  17.     With Sheets("学生成绩源")
  18.         Set c = .[a:a].Find(校, , , xlWhole)
  19.         If c Is Nothing Then
  20.             MsgBox 校 & "没有查到"
  21.             Exit Sub
  22.         End If
  23.         Set rng = c.Offset(, 2).Resize(.[a:a].Find(校, , , xlWhole, , xlPrevious).Row - c.Row + 1)
  24.         Set c2 = rng.Find(班, , , xlWhole)
  25.         If c2 Is Nothing Then
  26.             MsgBox 班 & "没有查到"
  27.             Exit Sub
  28.         End If
  29.         arr = c2.Resize(rng.Find(班, , , xlWhole, , xlPrevious).Row - c2.Row + 1, 5)
  30.     End With
  31.     n = 1
  32.     brr(1, 2) = 0
  33.     For i = 1 To UBound(arr)
  34.         hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
  35.         For j = 1 To n
  36.             If hj >= brr(j, 2) Then
  37.                 For m = n To j Step -1
  38.                     brr(m + 1, 1) = brr(m, 1)
  39.                     brr(m + 1, 2) = brr(m, 2)
  40.                 Next
  41.                 brr(j, 1) = i
  42.                 brr(j, 2) = hj
  43.                 Exit For
  44.             End If
  45.         Next
  46.         n = n + 1
  47.     Next
  48.     For i = 1 To n - 1
  49.         If i > 34 Then
  50.             x = i - 34
  51.             y = 8
  52.         Else
  53.             x = i
  54.             y = 0
  55.         End If
  56.         If brr(i, 2) <> brr(i - 1, 2) Then mc = i
  57.         crr(x, 1 + y) = mc
  58.         For j = 2 To 5
  59.             crr(x, j + y) = arr(brr(i, 1), j)
  60.         Next
  61.         crr(x, 6 + y) = brr(i, 2)
  62.     Next
  63.     Range("a5:o38") = crr
  64. End Sub

复制代码

TA的精华主题

TA的得分主题

发表于 2013-2-14 14:21 | 显示全部楼层
请看附件
查询.rar (45.79 KB, 下载次数: 17)

TA的精华主题

TA的得分主题

发表于 2013-2-14 14:25 | 显示全部楼层
tplk 发表于 2013-2-14 14:15
老师,我下载了这个二级数据有效性,第二级的下拉列表更新快,但我不会借鉴,您看看,好吗

这个好像也是我写的,不过是激活工作表时一次计算的,所以速度快些

TA的精华主题

TA的得分主题

发表于 2013-2-14 14:31 | 显示全部楼层
tplk 发表于 2013-2-14 14:15
老师,我下载了这个二级数据有效性,第二级的下拉列表更新快,但我不会借鉴,您看看,好吗
  1. Dim dicValidation As Object

  2. Private Sub Worksheet_Activate()
  3.     Call Macro1
  4. End Sub

  5. Sub Macro1()
  6.     Dim arr, i&, s$
  7.     With Sheets("学生成绩源")
  8.         arr = .Range("A2:C" & .Range("A65536").End(xlUp).Row)
  9.     End With
  10.     If dicValidation Is Nothing Then Set dicValidation = CreateObject("scripting.dictionary")
  11.     For i = 1 To UBound(arr)
  12.         If Not dicValidation.Exists(arr(i, 1)) Then Set dicValidation(arr(i, 1)) = CreateObject("scripting.dictionary")
  13.         dicValidation(arr(i, 1))(arr(i, 3)) = ""
  14.     Next
  15.     With [g1].Validation
  16.         .Delete
  17.         .Add 3, 1, 1, Join(dicValidation.keys, ",")
  18.     End With
  19. End Sub

  20. Private Sub Worksheet_Change(ByVal Target As Range)
  21.     If Target.Address(0, 0) <> "B3" And Target.Address(0, 0) <> "G1" Then Exit Sub
  22.     If dicValidation Is Nothing Then Call Macro1
  23.     If Target.Address(0, 0) = "G1" Then
  24.         With [b3].Validation
  25.             .Delete
  26.             If dicValidation.Exists(Target.Value) Then .Add 3, 1, 1, Join(dicValidation(Target.Value).keys, ",")
  27.         End With
  28.         [b3] = ""
  29.         Exit Sub
  30.     End If
  31.     Range("A5:O38").ClearContents
  32.     If [b3] = "" Then Exit Sub
  33.     Dim c As Range, c2 As Range, arr, i&, 校$, 班$, brr(100, 1 To 2), n&, rng As Range
  34.     Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
  35.     校 = Range("g1").Value
  36.     班 = Range("b3").Value
  37.     With Sheets("学生成绩源")
  38.         Set c = .[a:a].Find(校, , , xlWhole)
  39.         If c Is Nothing Then
  40.             MsgBox 校 & "没有查到"
  41.             Exit Sub
  42.         End If
  43.         Set rng = c.Offset(, 2).Resize(.[a:a].Find(校, , , xlWhole, , xlPrevious).Row - c.Row + 1)
  44.         Set c2 = rng.Find(班, , , xlWhole)
  45.         If c2 Is Nothing Then
  46.             MsgBox 班 & "没有查到"
  47.             Exit Sub
  48.         End If
  49.         arr = c2.Resize(rng.Find(班, , , xlWhole, , xlPrevious).Row - c2.Row + 1, 5)
  50.     End With
  51.     n = 1
  52.     brr(1, 2) = 0
  53.     For i = 1 To UBound(arr)
  54.         hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
  55.         For j = 1 To n
  56.             If hj >= brr(j, 2) Then
  57.                 For m = n To j Step -1
  58.                     brr(m + 1, 1) = brr(m, 1)
  59.                     brr(m + 1, 2) = brr(m, 2)
  60.                 Next
  61.                 brr(j, 1) = i
  62.                 brr(j, 2) = hj
  63.                 Exit For
  64.             End If
  65.         Next
  66.         n = n + 1
  67.     Next
  68.     For i = 1 To n - 1
  69.         If i > 34 Then
  70.             x = i - 34
  71.             y = 8
  72.         Else
  73.             x = i
  74.             y = 0
  75.         End If
  76.         If brr(i, 2) <> brr(i - 1, 2) Then mc = i
  77.         crr(x, 1 + y) = mc
  78.         For j = 2 To 5
  79.             crr(x, j + y) = arr(brr(i, 1), j)
  80.         Next
  81.         crr(x, 6 + y) = brr(i, 2)
  82.     Next
  83.     Range("a5:o38") = crr
  84. End Sub

复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-29 10:18 , Processed in 0.050606 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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