Excel精英培训网

 找回密码
 注册
12
返回列表 发新帖
楼主: liuguansky

[习题] 【字典201201班】B组(B01—B22)第3讲作业上交贴

  [复制链接]
发表于 2012-6-17 10:40 | 显示全部楼层
题1

  1.     Dim i, j, k As Integer
  2.     Dim arr, arr1(), arr2
  3.     Dim d
  4.     Set d = CreateObject("scripting.dictionary")
  5.     With Sheets("题1")
  6.         arr = .[a1].CurrentRegion
  7.         ReDim arr1(1 To UBound(arr), 1 To 3)
  8.         arr2 = .[d1].CurrentRegion
  9.         For i = 2 To UBound(arr2)
  10.             j = j + 1
  11.             d(arr2(i, 2)) = j
  12.             arr1(i - 1, 1) = j
  13.             arr1(i - 1, 2) = arr2(i, 2)
  14.         Next
  15.         
  16.         For k = 2 To UBound(arr)
  17.             If d.exists(arr(k, 1)) Then
  18.                 arr1(d(arr(k, 1)), 3) = arr1(d(arr(k, 1)), 3) + arr(k, 2)
  19.             Else
  20.                 j = j + 1
  21.                 d(arr(k, 1)) = j
  22.                 arr1(j, 1) = j
  23.                 arr1(j, 2) = arr(k, 1)
  24.                 arr1(j, 3) = arr(k, 2)
  25.             End If
  26.         Next
  27.         
  28.         .[d2].Resize(UBound(arr1), 3) = arr1
  29.         .Range(.Cells(UBound(arr2) + 1, 4), .Cells(.[e65536].End(xlUp).Row, 5)).Font.Bold = True
  30.         
  31.     End With
复制代码
题2

  1.     Dim i As Integer
  2.     Dim arr, arr1, arr2
  3.     Dim d
  4.     Set d = CreateObject("scripting.dictionary")
  5.     With Sheets("题2")
  6.         arr = .Range("a1:b" & .[b65536].End(xlUp).Row)
  7.         For i = 2 To UBound(arr)
  8.             If arr(i, 1) = "" Then arr(i, 1) = arr(i - 1, 1)
  9.             If d.exists(arr(i, 1)) And arr(i, 2) >= 100 And arr(i, 2) <= 400 Then
  10.                 d(arr(i, 1)) = d(arr(i, 1)) + arr(i, 2)
  11.             ElseIf arr(i, 2) >= 100 And arr(i, 2) <= 400 Then
  12.                 d(arr(i, 1)) = arr(i, 2)
  13.             End If
  14.         Next

  15.         arr1 = Application.Transpose(d.keys)
  16.         arr2 = Application.Transpose(d.items)
  17.         .[e2].Resize(UBound(arr1)) = arr1
  18.         .[f2].Resize(UBound(arr2)) = arr2
  19.     End With
复制代码

点评

第一题,很好;第二题,判断不用合并一起,注意层次就可以了。一个循环。不错的作业。  发表于 2012-6-20 12:56

评分

参与人数 1 +12 收起 理由
liuguansky + 12 7+5

查看全部评分

回复

使用道具 举报

发表于 2012-6-17 11:16 | 显示全部楼层
第3讲-B12-ゅ閪閪ヘ.zip (22.18 KB, 下载次数: 7)

点评

第一题,多次运行出错  发表于 2012-6-20 13:01

评分

参与人数 1 +8 收起 理由
liuguansky + 8 3+5

查看全部评分

回复

使用道具 举报

发表于 2012-6-19 09:47 | 显示全部楼层
Sub 第一题()
    Dim arr, hx As Long, arr1(1 To 999, 1 To 3)
    Dim y As Byte
    Dim d As New Dictionary
    With Range("f2:f9,d10:f100")
        .Font.Bold = False    '区域中的字体设为加粗
        .ClearContents    '数据表上单元格区域中的公式,但保留其格式设置
    End With
    hx = Range("e65536").End(xlUp).Row

    arr = Range("e2:e" & hx)
    For y = 1 To UBound(arr)
        d(arr(y, 1)) = y
        arr1(y, 1) = y
        arr1(y, 2) = arr(y, 1)
    Next y
    hx = Range("a65536").End(xlUp).Row

    arr = Range("a2:b" & hx)
    For hx = 1 To UBound(arr)
        If d.Exists(arr(hx, 1)) Then
            arr1(d(arr(hx, 1)), 3) = arr1(d(arr(hx, 1)), 3) + arr(hx, 2)
        Else
            d(arr(hx, 1)) = d.Count + 1
            arr1(d(arr(hx, 1)), 1) = d.Count
            arr1(d(arr(hx, 1)), 2) = arr(hx, 1)
            arr1(d(arr(hx, 1)), 3) = arr(hx, 2)
        End If
    Next
    Range("d2").Resize(d.Count, 3) = arr1
    Range("d" & y + 1).Resize(d.Count - y + 1, 2).Font.Bold = True
End Sub
Sub 第二题()
    Dim arr, arr1(1 To 999, 1 To 2)
    Dim x As Long
    Dim y As String
    Dim y1 As Long
    Dim y2 As Long
    Dim d As New Dictionary
    With Sheets("题2")
        y1 = .Range("d2")
        y2 = .Range("c2")
        .Range("e2:f100").ClearContents
        x = .Range("b65536").End(xlUp).Row
        arr = .Range("a2:b" & x)
        For x = 1 To UBound(arr)
            y = IIf(Len(arr(x, 1)) = 0, y, arr(x, 1))
            If arr(x, 2) >= y2 And arr(x, 2) <= y1 Then
                If d.Exists(y) Then
                    arr1(d(y), 2) = arr1(d(y), 2) + arr(x, 2)
                Else
                    d(y) = d.Count + 1
                    arr1(d(y), 1) = y
                    arr1(d(y), 2) = arr(x, 2)
                End If
            End If
        Next
        .Range("e2").Resize(d.Count, 2) = arr1
    End With
End Sub

点评

代码请用代码框贴出;建议D.COUNT用变量代替,增强可读性;思路很好。  发表于 2012-6-20 13:00

评分

参与人数 1 +11 收起 理由
liuguansky + 11

查看全部评分

回复

使用道具 举报

发表于 2012-6-20 11:44 | 显示全部楼层
  1. Sub test()
  2.     Dim d
  3.     Dim i As Integer
  4.     Dim c As Integer
  5.     Set d = CreateObject("scripting.dictionary")
  6.     For i = 2 To 9
  7.         d(Cells(i, "E").Value) = d.Count + 1
  8.     Next i
  9.     c = d.Count + 1
  10.     For i = 2 To Cells(Rows.Count, "a").End(xlUp).Row
  11.     If d.Exists(Cells(i, 1).Value) = False Then
  12.         d(Cells(i, 1).Value) = d.Count + 1
  13.         Cells(d(Cells(i, 1).Value) + 1, "d").Value = d(Cells(i, 1).Value)
  14.         Cells(d(Cells(i, 1).Value) + 1, "E").Value = Cells(i, 1).Value
  15.      End If
  16.         Cells(d(Cells(i, 1).Value) + 1, "F").Value = Cells(d(Cells(i, 1).Value) + 1, "F").Value + Cells(i, 2).Value
  17.     Next i
  18.     Range(Cells(c + 1, "d"), Cells(Rows.Count, "f").End(xlUp)).Font.Bold = True
  19.     Set d = Nothing
  20. End Sub
复制代码
  1. Sub test1()
  2.     Dim d
  3.     Dim i As Long
  4.     Dim arr()
  5.     Set d = CreateObject("scripting.dictionary")
  6.     arr = Range([a2], Cells(Rows.Count, "b").End(xlUp)).Value
  7.         For i = 1 To UBound(arr, 1)
  8.             If arr(i, 1) = "" Then
  9.                 arr(i, 1) = arr(i - 1, 1)
  10.             End If
  11.         d(arr(i, 1)) = arr(i, 2)
  12.             If d(arr(i, 1)) >= 100 And d(arr(i, 1)) <= 400 Then
  13.                 d(arr(i, 1)) = arr(i, 2) + d(arr(i, 1))
  14.             End If
  15.         Next i
  16.     Range("e2").Resize(d.Count, 1) = Application.WorksheetFunction.Transpose(d.Keys)
  17.     Range("f2").Resize(d.Count, 1) = Application.WorksheetFunction.Transpose(d.Items)
  18.     Set d = Nothing
  19. End Sub
复制代码

201201字典班第三讲作业.rar

12.6 KB, 下载次数: 7

点评

第一题,思路清晰;第二题多一句代码。  发表于 2012-6-20 12:58

评分

参与人数 1 +9 收起 理由
liuguansky + 9 5+4

查看全部评分

回复

使用道具 举报

发表于 2012-6-20 12:43 | 显示全部楼层
我不知道呀 发表于 2012-6-15 20:38
第一题代码:
Sub aa2()
    Dim d As New Dictionary

那天附件上传不了,所以就直接贴代码了
回复

使用道具 举报

发表于 2012-6-20 17:26 | 显示全部楼层
本帖最后由 7777 于 2012-6-20 17:45 编辑

B15:7777  只做出了第一题
Sub 第一题作业()
Dim D1 As New Dictionary
Dim ARR
Dim I As Long
ReDim ARR(1 To 8, 1 To 2)
Range("d10:f65535").ClearContents
Range("f2:f9").ClearContents
ARR = Range("D2:E9")
  For I = 1 To 8
  D1(ARR(I, 2)) = ARR(I, 1)
  Next
For I = 2 To Range("A65535").End(xlUp).Row
   If D1.Exists(Cells(I, 1).Value) = False Then
     D1(Cells(I, 1).Value) = D1.Count + 1
     Cells(D1.Count + 1, "D") = D1.Count
     Cells(D1.Count + 1, "e") = Cells(I, 1)
     Cells(D1.Count + 1, "f") = Cells(I, 2)
     Range("d10:f" & D1.Count + 2).Font.Bold = True
   Else
     Cells(D1(Cells(I, 1).Value) + 1, "F") = Cells(D1(Cells(I, 1).Value) + 1, "F") + Cells(I, 2)
   End If
  Next
End Sub
回复

使用道具 举报

发表于 2012-6-20 17:56 | 显示全部楼层
从从容容 发表于 2012-6-16 09:29

免费的,不用白不用。
回复

使用道具 举报

发表于 2012-6-20 18:08 | 显示全部楼层
真的太忙,没写作业请谅解!!!
回复

使用道具 举报

 楼主| 发表于 2012-6-20 19:09 | 显示全部楼层
参考代码:
  1. Option Explicit
  2. Sub Q1()
  3.     Dim D As New Dictionary, Arr, i&, Ar(1 To 60000, 1 To 3), K&, NAdd&
  4.     Arr = Range([e2], [e2].End(4)).Value
  5.     For i = 1 To UBound(Arr)
  6.         D.Add Arr(i, 1), i
  7.         Ar(i, 1) = i: Ar(i, 2) = Arr(i, 1)
  8.     Next i
  9.     K = i - 1: NAdd = K
  10.     Arr = Range([a2], [b2].End(4)).Value
  11.     For i = 1 To UBound(Arr)
  12.         If D.Exists(Arr(i, 1)) Then
  13.             Ar(D(Arr(i, 1)), 3) = Ar(D(Arr(i, 1)), 3) + Arr(i, 2)
  14.         Else
  15.             K = K + 1
  16.             D.Add Arr(i, 1), K
  17.             Ar(K, 1) = K: Ar(K, 2) = Arr(i, 1): Ar(K, 3) = Arr(i, 2)
  18.         End If
  19.     Next i
  20.     Range("d2:f" & Rows.Count).ClearContents
  21.     [d2].Resize(K, 3) = Ar
  22.     If K > NAdd Then [d2].Offset(NAdd).Resize(K - NAdd, 2).Font.Bold = True
  23.     Set D = Nothing
  24. End Sub
复制代码
  1. Option Explicit
  2. Sub Q2()
  3.     Dim D As New Dictionary, Arr, K&
  4.     Dim MaxN&, MinN&, i&, Ar(1 To 60000, 1 To 2)
  5.     MinN = [c2]: MaxN = [d2]
  6.     Arr = Range([a2], [b2].End(4)).Value
  7.     For i = 1 To UBound(Arr)
  8.         If Len(Arr(i, 1)) = 0 Then
  9.             Arr(i, 1) = Arr(i - 1, 1)
  10.         End If
  11.         If Arr(i, 2) >= MinN And Arr(i, 2) <= MaxN Then
  12.             If D.Exists(Arr(i, 1)) Then
  13.                 Ar(D(Arr(i, 1)), 2) = Ar(D(Arr(i, 1)), 2) + Arr(i, 2)
  14.             Else
  15.                 K = K + 1: D.Add Arr(i, 1), K
  16.                 Ar(K, 1) = Arr(i, 1): Ar(K, 2) = Arr(i, 2)
  17.             End If
  18.         End If
  19.     Next i
  20.     Range("e2:f" & Rows.Count).ClearContents
  21.     [e2].Resize(K, 2) = Ar
  22.     Set D = Nothing
  23. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-4 12:36 , Processed in 0.262446 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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