Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2413|回复: 3

[已解决]不能去重复答案又不对

[复制链接]
发表于 2014-8-5 19:42 | 显示全部楼层 |阅读模式
不能去重复答案又不对。
最佳答案
2014-8-5 21:58
  1. Sub test()
  2.    Dim arr, d As Object, re()
  3.    Dim irow&, icol%, product, price
  4.    arr = Range("B2:L" & Cells(Rows.Count, 2).End(xlUp).Row).Value
  5.    Set d = CreateObject("scripting.dictionary")
  6.    For irow = 1 To UBound(arr)
  7.        If Not d.exists(arr(irow, 1)) Then Set d(arr(irow, 1)) = CreateObject("scripting.dictionary")
  8.        For icol = 2 To UBound(arr, 2)
  9.            If Len(arr(irow, icol)) > 0 Then d(arr(irow, 1))(arr(irow, icol)) = ""
  10.        Next
  11.    Next
  12.    product = d.keys
  13.    ReDim re(1 To d.Count, 1 To 1)
  14.    For irow = 0 To UBound(product)
  15.        re(irow + 1, 1) = product(irow)
  16.        price = d(product(irow)).keys
  17.        If UBound(price) + 2 > UBound(re, 2) Then ReDim Preserve re(1 To UBound(re), 1 To UBound(price) + 2)
  18.        sortnum price
  19.        For icol = 0 To UBound(price)
  20.            re(irow + 1, icol + 2) = price(icol)
  21.        Next
  22.    Next
  23.    Range("N10").Resize(UBound(re), UBound(re, 2)) = re
  24. End Sub

  25. Sub sortnum(n)
  26.    Dim i&, j&, t
  27.    For i = 0 To UBound(n) - 1
  28.       For j = i + 1 To UBound(n)
  29.           If n(i) > n(j) Then
  30.              t = n(i)
  31.              n(i) = n(j)
  32.              n(j) = t
  33.           End If
  34.       Next
  35.    Next
  36. End Sub
复制代码

不能去重复答案又不对.rar

293.52 KB, 下载次数: 24

发表于 2014-8-5 21:58 | 显示全部楼层    本楼为最佳答案   
  1. Sub test()
  2.    Dim arr, d As Object, re()
  3.    Dim irow&, icol%, product, price
  4.    arr = Range("B2:L" & Cells(Rows.Count, 2).End(xlUp).Row).Value
  5.    Set d = CreateObject("scripting.dictionary")
  6.    For irow = 1 To UBound(arr)
  7.        If Not d.exists(arr(irow, 1)) Then Set d(arr(irow, 1)) = CreateObject("scripting.dictionary")
  8.        For icol = 2 To UBound(arr, 2)
  9.            If Len(arr(irow, icol)) > 0 Then d(arr(irow, 1))(arr(irow, icol)) = ""
  10.        Next
  11.    Next
  12.    product = d.keys
  13.    ReDim re(1 To d.Count, 1 To 1)
  14.    For irow = 0 To UBound(product)
  15.        re(irow + 1, 1) = product(irow)
  16.        price = d(product(irow)).keys
  17.        If UBound(price) + 2 > UBound(re, 2) Then ReDim Preserve re(1 To UBound(re), 1 To UBound(price) + 2)
  18.        sortnum price
  19.        For icol = 0 To UBound(price)
  20.            re(irow + 1, icol + 2) = price(icol)
  21.        Next
  22.    Next
  23.    Range("N10").Resize(UBound(re), UBound(re, 2)) = re
  24. End Sub

  25. Sub sortnum(n)
  26.    Dim i&, j&, t
  27.    For i = 0 To UBound(n) - 1
  28.       For j = i + 1 To UBound(n)
  29.           If n(i) > n(j) Then
  30.              t = n(i)
  31.              n(i) = n(j)
  32.              n(j) = t
  33.           End If
  34.       Next
  35.    Next
  36. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-8-5 22:07 | 显示全部楼层
本帖最后由 张雄友 于 2014-8-5 22:08 编辑
xdragon 发表于 2014-8-5 21:58

红色代码是什么意思?

Sub 去重复提取价格并从小到大排列()
   Dim arr, d As Object, brr(), irow&, icol%, product, price, tt#
   tt = Timer
   arr = Range("B2:L" & Cells(Rows.Count, 2).End(xlUp).Row).Value
   Set d = CreateObject("scripting.dictionary")
   For irow = 1 To UBound(arr)
       If Not d.exists(arr(irow, 1)) Then Set d(arr(irow, 1)) = CreateObject("scripting.dictionary")
       For icol = 2 To UBound(arr, 2)
           If Len(arr(irow, icol)) > 0 Then d(arr(irow, 1))(arr(irow, icol)) = ""
       Next
   Next
   product = d.keys
   ReDim brr(1 To d.Count, 1 To 1)
   For irow = 0 To UBound(product)
       brr(irow + 1, 1) = product(irow)
       price = d(product(irow)).keys
       If UBound(price) + 2 > UBound(brr, 2) Then ReDim Preserve brr(1 To UBound(brr), 1 To UBound(price) + 2)
       sortnum price
       For icol = 0 To UBound(price)
           brr(irow + 1, icol + 2) = price(icol)
       Next
   Next
   Range("N10").Resize(UBound(brr), UBound(brr, 2)) = brr
   MsgBox "用时" & Format(Timer - tt, "0.00") & "秒"
End Sub
Sub sortnum(n)
   Dim i&, j&, t
   For i = 0 To UBound(n) - 1
      For j = i + 1 To UBound(n)
          If n(i) > n(j) Then
             t = n(i)
             n(i) = n(j)
             n(j) = t
          End If
      Next
   Next
End Sub


回复

使用道具 举报

发表于 2014-8-5 22:08 | 显示全部楼层
张雄友 发表于 2014-8-5 22:07
红色代码是什么意思?

Sub 去重复提取价格并从小到大排列()

因为字典赋值到数组后,默认lbound=0,我的re数组的lbound=1 所以需要+1,另外,第一列不是放产品名称了吗?所以,就变成了+2了。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-14 16:56 , Processed in 0.473742 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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