Excel精英培训网

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

[习题] [字典与数组二期1班] 第2讲作业上交帖

  [复制链接]
发表于 2011-12-27 12:40 | 显示全部楼层
本帖最后由 JLxiangwei 于 2011-12-28 15:04 编辑

  1. Function Jc(rg As Range, Optional i As Long = 1)
  2.     Dim d As New Dictionary, d1 As New Dictionary, y&, arr, ar, s$
  3.     arr = rg
  4.     For Each ar In arr
  5.         d(ar) = d(ar) + 1
  6.     Next
  7.     For y = 0 To d.Count - 1
  8.         d1(d.Items(y)) = ""
  9.     Next y
  10.     n = Application.Large(d1.Keys, i)
  11.     If i > d1.Count Then Exit Function
  12.     For y = 0 To d.Count - 1
  13.         If d.Items(y) = n Then
  14.             s = s & d.Keys(y) & "|"
  15.         End If
  16.     Next y
  17. Jc = Left(s, Len(s) - 1)
  18. End Function



  19. Sub 必做二()
  20.     Dim sh As Worksheet, d As New Dictionary, x&, arr
  21.     With Sheets("必做二")
  22.         For Each sh In Sheets
  23.             If sh.Name <> "必做二" And sh.Tab.ColorIndex = .Tab.ColorIndex Then
  24.                 arr = sh.Range("a2:b" & sh.Cells(sh.Rows.Count, 2).End(3).Row)
  25.                 For x = 1 To UBound(arr)
  26.                     d(arr(x, 1)) = d(arr(x, 1)) + Val(arr(x, 2))
  27.                 Next x
  28.             End If
  29.         Next
  30.         .Range("a2:b" & Rows.Count).ClearContents
  31.         .[a1] = "产品型号"
  32.         .[b1] = "数量"
  33.         .[a2].Resize(d.Count) = Application.Transpose(d.Keys)
  34.         .[b2].Resize(d.Count) = Application.Transpose(d.Items)
  35.     End With
  36. End Sub


  37. Sub 选做一()
  38.     Dim d As New Dictionary, arr, x&, str$, str1$, arr1
  39.     With Sheets("选做一")
  40.         arr = .Range("a2:b" & .Cells(.Rows.Count, 2).End(3).Row).Formula
  41.         str = Range("E1")
  42.         For x = 1 To UBound(arr)
  43.             If arr(x, 1) = str Then
  44.                 str1 = str1 & Mid(arr(x, 2), 2, 20) & "+"
  45.             End If
  46.         Next
  47.         arr1 = VBA.Split(str1, "+")
  48.         .Range("e2:e" & Rows.Count).ClearContents
  49.         .[e2].Resize(UBound(arr1)) = Application.Transpose(arr1)
  50.     End With
  51. End Sub


  52. Sub 选做二()
  53.     Dim d As New Dictionary, arr, x&, arr1(1 To 10, 1 To 7), y&
  54.     arr = Sheet11.Range("a1:g" & Sheet11.Cells(Sheet11.Rows.Count, 6).End(3).Row)
  55.     Do
  56.         x = Rnd() * (UBound(arr) - 1) + 1
  57.         d(x) = ""
  58.     Loop Until d.Count = 10
  59.     For x = 1 To 10
  60.         For y = 1 To 7
  61.             arr1(x, y) = arr(d.Keys(x - 1), y)
  62.         Next y
  63.     Next x
  64.     Sheet5.Range("a1").Resize(10, 7) = arr1
  65. End Sub




  66. Sub 附加题()
  67.     Dim d As New Dictionary, d1 As New Dictionary, sht As Worksheet
  68.     Dim arr, i&, k&, arr1, m&, j&, str1$, t, n&
  69.     t = Timer
  70.     k = Sheet10.Cells(Sheet10.Rows.Count, 2).End(3).Row
  71.     arr = Sheet10.Range("a1:s" & k).Value
  72.     For i = 2 To UBound(arr)
  73.         d(arr(i, 14)) = ""
  74.     Next
  75.     For Each sht In Sheets
  76.         d1(sht.Name) = ""
  77.     Next
  78.     On Error Resume Next
  79.     Application.ScreenUpdating = False
  80.     Application.DisplayAlerts = False
  81.     For i = 0 To UBound(d.Keys)
  82.         If d1.Exists(d.Keys(i)) Then
  83.             str1 = MsgBox(d.Keys(i) & "工作表已存在,是否删除?", vbYesNo)
  84.             If str1 = vbYes Then
  85.                 Sheets(d.Keys(i)).Delete
  86.             Else
  87.                 GoTo 100
  88.             End If
  89.         End If
  90.         Sheets.Add after:=Sheets(Sheets.Count)
  91. 100
  92.         Sheets(d.Keys(i)).Select
  93.         With ActiveSheet
  94.             .Name = d.Keys(i)
  95.             m = 0
  96.             ReDim arr1(1 To k, 1 To UBound(arr, 2))
  97.             For j = 2 To UBound(arr, 1)
  98.                 If arr(j, 14) = d.Keys(i) Then
  99.                     m = m + 1
  100.                     For n = 1 To UBound(arr, 2)
  101.                         arr1(m, n) = arr(j, n)
  102.                     Next n
  103.                 End If
  104.             Next j
  105.             .[a1:s1] = Application.Index(arr, 1)
  106.             .[a2].Resize(k, UBound(arr, 2)) = arr1
  107.         End With
  108.     Next i
  109.     Application.DisplayAlerts = True
  110.     Application.ScreenUpdating = True
  111. End Sub


复制代码

点评

OK  发表于 2011-12-27 18:59
回复

使用道具 举报

发表于 2011-12-27 16:00 | 显示全部楼层
本帖最后由 9lee 于 2011-12-27 16:06 编辑
  1. Private Function Jc(rng, Optional n As Long = 1)'9lee
  2.     Dim arr, d As New Dictionary, nMax, a, i, j, x, dd As New Dictionary
  3.    
  4.     On Error Resume Next
  5.     '======第一参数不是数组时,显示错误值
  6.     If IsArray(rng) = False Then Jc = "#VALUE!": Exit Function
  7.     arr = rng
  8.     '========确定数组的维数
  9.     For x = 1 To 64
  10.         If IsEmpty(UBound(arr, x)) Then Exit For
  11.     Next
  12.     '========统计各元素出现的次数
  13.     If x = 2 Then   '一维时
  14.         For i = 1 To UBound(arr)
  15.             If IsEmpty(arr(i)) = False Then d(arr(i)) = d(arr(i)) + 1
  16.         Next
  17.     Else   '二维时
  18.         For i = 1 To UBound(arr, 1)
  19.             For j = 1 To UBound(arr, 2)
  20.                 If IsEmpty(arr(i, j)) = False Then d(arr(i, j)) = d(arr(i, j)) + 1
  21.             Next j
  22.         Next i
  23.     End If
  24.     '======统计出现次数的值
  25.     For i = 0 To d.Count - 1
  26.         dd(d.Items(i)) = ""
  27.     Next
  28.     '===如果第二参数大于次数的值的个数,则显示空值
  29.     If n > dd.Count Then Jc = "": Exit Function
  30.     '====
  31.     nMax = Application.Large(dd.Keys, n)
  32.     '====获取出现N次的元素
  33.     For i = 0 To d.Count - 1
  34.         If d.Items(i) = nMax Then a = a & "|" & d.Keys(i)
  35.     Next
  36.     Jc = Mid(a, 2, 10000)
  37.     Set d = Nothing
  38.     Set dd = Nothing
  39. End Function
复制代码
  1. Sub 必做二_9lee()
  2.     Dim d As New Dictionary, arr, i, sh As Worksheet, a
  3.     With Sheets("必做二")
  4.         a = Sheets("必做二").Tab.ColorIndex
  5.         For Each sh In ThisWorkbook.Sheets
  6.             If sh.Tab.ColorIndex = a And sh.Name <> "必做二" Then
  7.                 arr = sh.Range("a1").CurrentRegion
  8.                 For i = 2 To UBound(arr)
  9.                     d(arr(i, 1)) = d(arr(i, 1)) + arr(i, 2) * 1
  10.                 Next i
  11.             End If
  12.         Next
  13.         .Range("a:b").ClearContents
  14.         .Range("a1") = "产品型号"
  15.         .Range("b1") = "数量"
  16.         If d.Count > 0 Then
  17.             .Range("a2:a" & d.Count) = Application.Transpose(d.Keys)
  18.             .Range("b2:b" & d.Count) = Application.Transpose(d.Items)
  19.         End If
  20.     End With
  21.     Set d = Nothing
  22. End Sub
复制代码
  1. Sub 选做二_9lee()
  2.     Dim brr, d As New Dictionary
  3.     Dim a, irow, i
  4.     t = Timer
  5.     With Sheets("选做二数据源")
  6.     a = .[a65536].End(3).Row
  7.     Do
  8.         irow = Int(a * Rnd + 1)
  9.         If Not d.Exists(d(irow)) Then
  10.            d(irow) = .Cells(irow, 1).EntireRow
  11.         End If
  12.         i = i + 1
  13.     Loop Until i = 10
  14.     End With
  15.     brr = Application.Transpose(Application.Transpose(d.Items))
  16.     With Sheets("选做二")
  17.         .Range("a1").CurrentRegion.ClearContents
  18.         .[a1].Resize(10, UBound(brr, 2)) = brr
  19.     End With
  20.     Set d = Nothing
  21.     MsgBox Timer - t
  22. End Sub
复制代码
  1. Sub 选做一_9lee()
  2. Dim arr, brr, chaxun As String, d As New Dictionary
  3. With Sheets("选做一")
  4. .[e2:e65536].ClearContents
  5. chaxun = UCase(.[e1])
  6. arr = Range("a1").CurrentRegion.Formula
  7. For i = 2 To UBound(arr)
  8. If UCase(arr(i, 1)) Like chaxun Then
  9. d(i) = Mid(arr(i, 2), 2, 10000)
  10. End If
  11. Next

  12. If d.Count > 0 Then
  13. brr = Split(Join(d.Items, "+"), "+")
  14. .[e2].Resize(UBound(brr)) = Application.Transpose(brr)
  15. Else
  16. MsgBox .[e1] & "没有找到!"
  17. End If
  18. End With
  19. Set d = Nothing
  20. End Sub
复制代码
  1. Sub 附加题_9lee()
  2. Dim arr, brr, crr(), d As New Dictionary, d_Name As New Dictionary, dd As New Dictionary
  3. Dim i, j, my, k, m
  4. Application.ScreenUpdating = False
  5. t = Timer
  6. With Sheets("附加题")
  7. arr = .Range("a1").CurrentRegion
  8. brr = .Rows(1)
  9. '=============获取拆分项目
  10. For i = 2 To UBound(arr, 1)
  11. d(arr(i, 14)) = ""
  12. Next
  13. '============获取已有的工作表名称
  14. For Each sh In ThisWorkbook.Sheets
  15. d_Name(sh.Name) = ""
  16. Next
  17. '=========拆分
  18. For i = 0 To d.Count - 1
  19. Set sh = Sheets.Add(after:=Sheets(ThisWorkbook.Sheets.Count))
  20. '==========判断工作表是否存在,选择是否需要删除
  21. If d_Name.Exists(d.Keys(i)) Then
  22. my = MsgBox(d.Keys(i) & "的工作表已存在,是否需要删除该工作表?", vbYesNo)
  23. If my = vbYes Then
  24. Application.DisplayAlerts = False
  25. Sheets(d.Keys(i)).Delete
  26. sh.Name = d.Keys(i)
  27. Application.DisplayAlerts = True
  28. End If
  29. Else
  30. sh.Name = d.Keys(i)
  31. End If
  32. ReDim crr(1 To UBound(arr), 1 To UBound(arr, 2))
  33. For j = 2 To UBound(arr)
  34. If arr(j, 14) = d.Keys(i) Then
  35. For k = 1 To UBound(arr, 2)
  36. crr(m + 1, k) = arr(j, k)
  37. Next k
  38. m = m + 1
  39. End If

  40. Next
  41. sh.[a1].Resize(1, UBound(brr, 2)) = brr
  42. sh.[a2].Resize(UBound(crr, 1), UBound(crr, 2)) = crr
  43. Set dd = Nothing
  44. m = 0
  45. Next
  46. End With
  47. Set d = Nothing
  48. Set d_Name = Nothing
  49. MsgBox "拆分完毕!用时" & Timer - t & "秒."
  50. Application.ScreenUpdating = True
  51. End Sub
复制代码

9lee作业2.rar

1.39 MB, 下载次数: 25

点评

ok  发表于 2011-12-27 19:02
回复

使用道具 举报

发表于 2011-12-27 18:49 | 显示全部楼层
  1. Function jc(arr, Optional n% = 1) As String  '必做一
  2. Dim d As New Dictionary
  3. If TypeName(arr) = "Range" Then
  4. arr = arr.Value
  5. For i = LBound(arr) To UBound(arr)
  6. For j = LBound(arr, 2) To UBound(arr, 2)
  7. If d.Exists(arr(i, j)) Then
  8. d(arr(i, j)) = d(arr(i, j)) + 1
  9. Else
  10. d.Add arr(i, j), 1
  11. End If
  12. Next j
  13. Next i
  14. Else
  15. For i = LBound(arr) To UBound(arr)
  16. d(arr(i)) = d(arr(i)) + 1
  17. Next i
  18. End If
  19. For k = 0 To d.Count - 1
  20. If d.Items(k) = n Then
  21. If jc = "" Then
  22. jc = d.Keys(k)
  23. Else
  24. jc = jc & "|" & d.Keys(k)
  25. End If
  26. End If
  27. Next k
  28. End Function[code]
  29. Sub huizong()  '必做二
  30. Dim d As New Dictionary
  31. Dim sht As Worksheet
  32. Dim a%, b&, arr
  33. Sheets("必做二").Select
  34. a = Me.Tab.ColorIndex
  35. For Each sht In Sheets
  36. If sht.Name <> "必做二" Then
  37. If sht.Tab.ColorIndex = a Then
  38. If Not IsEmpty(arr) Then Erase arr
  39. b = 0
  40. b = sht.Cells(Rows.Count, 1).End(3).Row
  41. arr = sht.Range("a2:b" & b).Value
  42. For i = 1 To b - 1
  43. For j = 1 To 2
  44. d(arr(i, 1)) = d(arr(i, 1)) + Val(arr(i, 2))
  45. Next j
  46. Next i
  47. End If
  48. End If
  49. Next sht
  50. [a1:b1] = Array("产品型号", "汇总数量")
  51. [a2].Resize(d.Count, 1) = Application.Transpose(d.Keys)
  52. [b2].Resize(d.Count, 1) = Application.Transpose(d.Items)
  53. End Sub
复制代码
[/code]
  1. Sub 选做一()
  2. Dim d As New Dictionary
  3. Dim a$, aa$, bb$, arr, brr
  4. Sheets("选做一").Select
  5. arr = Range("a2:a" & Cells(Rows.Count, 1).End(3).Row)
  6. For i = 1 To UBound(arr)
  7. d(arr(i, 1)) = d(arr(i, 1)) & Cells(i + 1, 2).Formula
  8. Next i
  9. a = d(Trim(Cells(1, 5)))
  10. If Not IsEmpty(a) Then
  11. aa = Replace(a, "=", "+")
  12. bb = Replace(aa, "+", "", , 1)
  13. brr = Split(bb, "+")
  14. [e2].Resize(UBound(brr) + 1, 1).ClearComments
  15. [e2].Resize(UBound(brr) + 1, 1) = Application.Transpose(brr)
  16. Else
  17. MsgBox "此产品型号不存在!"
  18. End If
  19. End Sub
复制代码
  1. Sub 选做二()
  2. Dim arr(1 To 10, 1 To 7), a(1 To 10) As Long, b&, m&
  3. Sheets("选做二数据源").Select
  4. m = Cells(Rows.Count, 1).End(3).Row
  5. For i = 1 To 10
  6. again:
  7. b = Rnd * m + 1
  8.     For j = 1 To i - 1
  9.     If b = a(j) Then GoTo again
  10.     Next j
  11. a(i) = b
  12.     For j = 1 To 7
  13.      arr(i, j) = Cells(b, j)
  14.     Next j
  15. Next i
  16. Sheets("选做二").Select
  17. [a1:g10].ClearComments
  18. [a1:g10] = arr
  19. End Sub
复制代码
  1. Sub 拆分()  '附加题
  2. Dim arr, brr, m&, a%, ss$
  3. Sheets("附加题").Select
  4. Application.ScreenUpdating = False
  5. m = Cells(Rows.Count, 14).End(3).Row
  6. arr = Range("n2:n" & m)
  7. Dim d As New Dictionary
  8. For i = 1 To m - 1
  9. If Not d.Exists(arr(i, 1)) Then
  10. d.Add arr(i, 1), i + 1
  11. Else
  12. d(arr(i, 1)) = d(arr(i, 1)) & "|" & i + 1
  13. End If
  14. Next i
  15. For i = 0 To d.Count - 1
  16. a = 0
  17. ss = d.Keys(i)
  18.     For j = 1 To Sheets.Count
  19.     If Sheets(j).Name = ss Then a = MsgBox("表名已经存在,是否删除已有工作表?", 4 + 32 + 0 + 0, "请选择")
  20.     If a = 6 Then Sheets(ss).Delete
  21.     Next j
  22. If a <> 7 Then
  23. Sheets.Add after:=Sheets(Sheets.Count)
  24. ActiveSheet.Name = ss
  25. [a1:s1].Value = Sheets("附加题").[a1:s1].Value
  26. ps = Split(d.Items(i), "|")
  27. t = UBound(ps)
  28. ReDim brr(t)
  29. Sheets("附加题").Select
  30.     For k = 0 To t
  31.     brr(k) = Range(Cells(ps(k), "A"), Cells(ps(k), "S"))
  32.     Next k
  33. Sheets(ss).Select
  34.     For w = 2 To t + 2
  35.     Range(Cells(w, 1), Cells(w, 19)) = brr(w - 2)
  36.     Next w
  37.     Columns("A:S").AutoFit
  38. Erase brr, ps
  39. End If
  40. Next i
  41. Application.ScreenUpdating = True
  42. End Sub
复制代码
回复

使用道具 举报

发表于 2011-12-27 23:10 | 显示全部楼层
第2讲作业上交,请老师批改。附加题暂未做。

  1. rem 必做一,函数
  2. Public Function Ldxhzy(Sou As Range, Optional Cs As Long = 1) As String
  3.     Rem ò
  4.     Dim MyArr(), TmpK(), TmpI(), Tmp()
  5.     Dim MyDic As New Dictionary
  6.     Dim I As Long, J As Long
  7.     Dim M As Long
  8.     Dim TmpStr As String
  9.     I = Sou.Rows.Count
  10.     J = Sou.Columns.Count
  11.     ReDim MyArr(1 To I, 1 To J)
  12.     MyArr = Sou
  13.      
  14.     For I = 1 To UBound(MyArr, 1)    'à×
  15.         For J = 1 To UBound(MyArr, 2)
  16.             If Not IsEmpty(MyArr(I, J)) Then   'í
  17.                 If MyDic.Exists(MyArr(I, J)) Then
  18.                     MyDic.Item(MyArr(I, J)) = MyDic.Item(MyArr(I, J)) + 1
  19.                  Else
  20.                     MyDic.Add MyArr(I, J), 1
  21.                 End If
  22.             End If
  23.         Next J
  24.     Next I
  25.     ReDim Tmp(1 To MyDic.Count, 1 To 2)
  26.     TmpK = MyDic.Keys   'TmpK±ê 
  27.     TmpI = MyDic.Items
  28.     For I = 0 To UBound(TmpK)   '××é
  29.         Tmp(I + 1, 1) = TmpK(I)
  30.         Tmp(I + 1, 2) = Val(TmpI(I))
  31.     Next I
  32.     For I = 1 To UBound(Tmp, 1)  '°óò
  33.         TmpStr = Tmp(I, 1)
  34.         M = Tmp(I, 2)
  35.         For J = I + 1 To UBound(Tmp, 1)
  36.             If M < Tmp(J, 2) Then   '
  37.                 Tmp(I, 1) = Tmp(J, 1)
  38.                 Tmp(I, 2) = Tmp(J, 2)
  39.                 Tmp(J, 1) = TmpStr
  40.                 Tmp(J, 2) = M
  41.             End If
  42.         Next J
  43.     Next I
  44.     M = Application.WorksheetFunction.Large(TmpI, Cs)
  45.     Ldxhzy = ""
  46.     For I = 1 To UBound(Tmp, 1)
  47.         If Tmp(I, 2) = M Then
  48.             Ldxhzy = Ldxhzy & "|" & Tmp(I, 1)
  49.         End If
  50.     Next I
  51.     Ldxhzy = IIf(Len(Ldxhzy) > 1, Mid(Ldxhzy, 2), "")
  52. End Function
复制代码
  1. rem 必做二,过程代码
  2. Private Sub CommandButton1_Click()
  3.     Dim Ys As Long
  4.     Dim I As Long
  5.     Dim Ws As Worksheet
  6.     Dim Sou(), Tmp(), Xh(), Sl()
  7.     Dim MyDic As New Dictionary
  8.     Ys = ActiveSheet.Tab.ColorIndex
  9.     For Each Ws In ThisWorkbook.Sheets
  10.         If (Ws.Tab.ColorIndex = Ys) And (Ws.Name <> Me.Name) Then
  11.             I = Ws.UsedRange.Rows.Count
  12.             Tmp = Ws.Range("A2:B" & I)
  13.             For I = 1 To UBound(Tmp, 1)
  14.                 If MyDic.Exists(Tmp(I, 1)) Then
  15.                     MyDic.Item(Tmp(I, 1)) = MyDic.Item(Tmp(I, 1)) + Val(Tmp(I, 2))
  16.                 Else
  17.                     MyDic.Item(Tmp(I, 1)) = Val(Tmp(I, 2))
  18.                 End If
  19.              Next I
  20.         End If
  21.     Next
  22.     Xh = MyDic.Keys
  23.     Sl = MyDic.Items
  24.     I = UBound(Xh) - LBound(Xh) + 1
  25.     Range("A:B").ClearContents
  26.     ActiveSheet.Range("A1").Resize(1, 2) = Array("ú·", "")
  27.     ActiveSheet.Range("A2").Resize(I, 1) = Application.WorksheetFunction.Transpose(Xh)
  28.     ActiveSheet.Range("B2").Resize(I, 1) = Application.WorksheetFunction.Transpose(Sl)

  29. End Sub
复制代码
  1. rem 选做一,代码

  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     If Target.Address = "$E$1" Then
  4.         Dim MyArr(), MyTag()
  5.         Dim I As Long
  6.         I = UsedRange.Rows.Count
  7.         ReDim MyArr(1 To I - 1, 1 To 2)
  8.         MyArr = Range("A2:B" & I).Formula
  9.         For I = 1 To UsedRange.Rows.Count - 1
  10.             If MyArr(I, 1) = Target.Value Then
  11.                 ReDim Preserve MyTag(1 To I)
  12.                 MyTag(I) = "'" & MyArr(I, 2)
  13.             End If
  14.         Next I
  15.         I = UBound(MyTag)
  16.         Range("E2:E" & UsedRange.Rows.Count).Resize(I, 1) = Application.WorksheetFunction.Transpose(MyTag)
  17.       
  18.     End If
  19.    
  20. End Sub
复制代码
  1. rem 选 做二,过程代码
  2. Private Sub CommandButton1_Click()
  3.     Dim I As Long, M As Long
  4.     Dim MyDic As New Dictionary
  5.     Dim Tmp()
  6.     I = ThisWorkbook.Sheets("×").UsedRange.Rows.Count
  7.     While MyDic.Count < 10
  8.         M = Int(Rnd() * I + 1)
  9.         MyDic(M) = M
  10.     Wend
  11.     Tmp = MyDic.Items
  12.     For I = 0 To 9
  13.         Range("A" & (I + 1)).Resize(1, 7) = ThisWorkbook.Sheets("×").Range("A" & Tmp(I) & ":G" & Tmp(I)).Value
  14.     Next I
  15. End Sub
复制代码


回复

使用道具 举报

发表于 2011-12-27 23:56 | 显示全部楼层
近段时间忙,第一题不会做,只做了第二题,先上交了。
字典班第2讲作业2023-jsgslgd.zip (1.76 MB, 下载次数: 11)
回复

使用道具 举报

发表于 2011-12-28 10:14 | 显示全部楼层
报告老师,一道题都不会做。
回复

使用道具 举报

发表于 2011-12-28 11:20 | 显示全部楼层
本帖最后由 swabe 于 2011-12-28 14:30 编辑


  1. '必做一
  2. Function jc(a As Variant, Optional b As Long = 1)
  3.     Application.Volatile
  4.     Dim arr, arrtmp
  5.     Dim dic As New Dictionary
  6.     Dim arrdata
  7.     Dim x&, i&, y$
  8.     arr = a
  9.     For Each arrdata In arr
  10.         dic(arrdata) = dic(arrdata) + 1
  11.     Next
  12.     arrtmp = dic.Items
  13.     x = Application.WorksheetFunction.Large(arrtmp, b)
  14.     For i = 0 To dic.Count - 1
  15.         If dic(dic.Keys(i)) = x Then
  16.             y = y & "|" & dic.Keys(i)
  17.         End If
  18.     Next
  19.     jc = Mid(y, 2, Len(y) - 1)
  20. End Function
复制代码


  1. Sub swabe_必做二()
  2.     Dim dic As New Dictionary
  3.     Dim sht As Worksheet
  4.     Dim i As Long, x As Long
  5.     Dim arr, arrtmp, arrtmp2
  6.     x = Sheet3.Tab.Color
  7.     For Each sht In Worksheets
  8.         If sht.Name <> Sheet3.Name And sht.Tab.Color = x Then
  9.             arr = sht.Range("a2:B" & sht.Range("a65536").End(xlUp).Row)
  10.             For i = 1 To UBound(arr)
  11.                 dic(arr(i, 1)) = dic(arr(i, 1)) + arr(i, 2) * 1
  12.             Next
  13.         End If
  14.     Next
  15.     arrtmp = dic.Keys
  16.     arrtmp2 = dic.Items
  17.     Sheet3.Range("A1").Resize(dic.Count, 1) = Application.Transpose(arrtmp)
  18.     Sheet3.Range("b1").Resize(dic.Count, 1) = Application.Transpose(arrtmp2)
  19. End Sub
复制代码


  1. '选做一 (工作表代码)
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     If Target.Address <> "$E$1" Then Exit Sub
  4.     Dim dic As New Dictionary
  5.     Dim arr
  6.     Dim i&, x$
  7.     x = Target.Value
  8.     arr = Range("A2:B" & Range("A65536").End(xlUp).Row).Formula
  9.     For i = 1 To UBound(arr)
  10.         dic(arr(i, 1)) = dic(arr(i, 1)) & "+" & Mid(arr(i, 2), 2, Len(arr(i, 2)))
  11.     Next
  12.     arr = Application.Transpose(Split(Mid(dic(x), 2, Len(dic(x))), "+"))
  13.     Range("E2").Resize(UBound(arr)) = arr
  14. End Sub
复制代码


  1. Sub swabe_选做二()
  2.     Dim arr, arrtmp(1 To 10, 1 To 7)
  3.     Dim dic As New Dictionary
  4.     Dim x&, y&, i%, j%
  5.    
  6.     arr = Sheet11.Range("A1:G" & Range("A65536").End(xlUp).Row)
  7.      x = UBound(arr)

  8.     Do
  9.         y = Int(Rnd() * x + 1)
  10.         dic(y) = ""
  11.     Loop Until dic.Count = 10
  12.     For i = 1 To 10
  13.         For j = 1 To 7
  14.             arrtmp(i, j) = arr(dic.Keys(i - 1), j)
  15.         Next
  16.     Next
  17.     Sheet11.Range("A1").Resize(10, 7) = arrtmp
  18. End Sub
复制代码



  1. Sub swabe_附加题()
  2.     Application.ScreenUpdating = False
  3.     Dim dic_sht As New Dictionary
  4.     Dim dic As New Dictionary
  5.     Dim sht As Worksheet
  6.     Dim i&, x&, y%, j&
  7.     Dim arrdata, arr_top, arrtmp
  8.     Dim pd As Boolean
  9.     For Each sht In Sheets
  10.         dic_sht(sht.Name) = ""
  11.     Next
  12.     With Sheets("附加题")
  13.         arrdata = .Range("A2:S" & .Range("N65536").End(xlUp).Row)
  14.         arr_top = .Range("A1:S1")
  15.     End With
  16.     '
  17.     For i = 1 To UBound(arrdata)
  18.         dic(arrdata(i, 14)) = ""
  19.     Next
  20.     For i = 0 To dic.Count - 1
  21.         If dic_sht.Exists(dic.Keys(i)) Then
  22.             pd = MsgBox("工作表" & dic(i) & "己存在!", vbYesNo)
  23.             If pd = vbYes Then
  24.                 Application.DisplayAlerts = False
  25.                 Sheets(dic(dic(i))).Delete
  26.                 Sheets.Add(after:=Sheets.Count).Name = dic.Keys(i)
  27.                 Application.DisplayAlerts = True
  28.             End If
  29.         Else
  30.             Worksheets.Add(after:=Sheets(Sheets.Count)).Name = dic.Keys(i)
  31.         End If
  32.         ReDim arrtmp(1 To UBound(arrdata), 1 To 19)
  33.         x = 1
  34.         For j = 2 To UBound(arrdata)
  35.             If arrdata(j, 14) = dic.Keys(i) Then
  36.                 For y = 1 To 19
  37.                     arrtmp(x, y) = arrdata(j, y)
  38.                 Next
  39.                 x = x + 1
  40.             End If
  41.         Next
  42.         With Sheets("附加题")
  43.             Sheets(dic.Keys(i)).Range("A1:S1") = arr_top
  44.             Sheets(dic.Keys(i)).Range("A2").Resize(UBound(arrtmp), 19) = arrtmp
  45.         End With
  46.     Next
  47.     Application.ScreenUpdating = True
  48. End Sub
复制代码

回复

使用道具 举报

发表于 2011-12-28 15:14 | 显示全部楼层
001-那么的帅-作业2.rar (1.37 MB, 下载次数: 38)

点评

那么的帅 做出来的当然是很帅的咯  发表于 2011-12-28 18:06
回复

使用道具 举报

发表于 2011-12-28 18:14 | 显示全部楼层
只做了第一题,感觉挺难,也没调试通过,总提示VBA.Filter不正确,希望老师能指导下。

Function jc(ByRef arr, ByVal dj As Integer) As String
  Dim d, d2, arr2, arr3
  
  Dim i As Integer
  
  Set d = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1

  For i = 1 To arr.Count
    d(arr(i)) = d(arr(i)) + 1
  Next
  
  arr2 = d.Items
  
  For i = 1 To arr2.Count
    d2(arr2(i)) = ""
  Next

  djval = Application.Large(d2.Keys, dj)
  arr3 = VBA.Filter(d.Keys, djval & "")
  jc = arr3
  

  
End Function








回复

使用道具 举报

发表于 2011-12-28 18:34 | 显示全部楼层
郁闷死了,一题都不会做,我基础太差了,再加年底很忙,也来不及去复习!很对不起老师苦心教导!
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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