Board logo

標題: [發問] 依篩選條件輸出明細並統計 [打印本頁]

作者: b9208    時間: 2012-9-30 21:42     標題: 依篩選條件輸出明細並統計

請教先進!
依附件檔中
〞LIST〞工作表中欄位〞物品〞(H)單項以〞+〞分開,且出貨時間(K)之日期及班別(P)相同,則只統計一筆資料。
篩選出之資料輸出於〞明細〞工作表中,並依「物品單項」及「日期」排序。
請參考附檔。
謝謝指導

[attach]12651[/attach]
作者: Hsieh    時間: 2012-10-1 00:17

回復 1# b9208
  1. Sub ex()
  2. Set dic = CreateObject("Scripting.Dictionary")
  3. Set dic1 = CreateObject("Scripting.Dictionary")
  4. Set dic2 = CreateObject("Scripting.Dictionary")

  5. With Sheets("List")
  6. For Each a In .Range(.[H6], .[H65536].End(xlUp)).SpecialCells(xlCellTypeConstants)
  7. ar = Split(a, "+")
  8.    For Each c In ar
  9.       mystr = c & "," & Left(a.Offset(, 3), 8) & "," & a.Offset(, 8)
  10.       dic1(mystr) = Split(mystr, ",")
  11.    Next
  12. Next
  13. End With
  14. With Sheets("明細")
  15. ay = Application.Transpose(Application.Transpose(dic1.items))
  16. For i = 1 To UBound(ay, 1)
  17.    mystr = ay(i, 1) & ay(i, 2)
  18.    dic2(ay(i, 1)) = dic2(ay(i, 1)) + 1
  19.    If IsEmpty(dic(mystr)) Then
  20.      ary = Array(ay(i, 1), ay(i, 2), 1)
  21.    Else
  22.      ary = dic(mystr)
  23.      ary(2) = ary(2) + 1
  24.    End If
  25.    dic(mystr) = ary
  26. Next
  27. With .[B3].Resize(dic.Count, 3)
  28. .Value = Application.Transpose(Application.Transpose(dic.items))
  29. .Sort key1:=.Cells(1, 1), header:=xlNo
  30. For Each a In .Columns(1).Cells
  31. If a <> a.Offset(-1, 0) Then a.Offset(, 3) = dic2(a.Value)
  32. Next
  33. End With
  34. End With
  35. End Sub
複製代碼

作者: softsadwind    時間: 2012-10-1 15:48

回復 2# Hsieh


    謝謝 好用的想法 和解答
    拾起 以後有機會可以用...
作者: b9208    時間: 2012-10-1 22:05

回復 2# Hsieh
非常感謝先進
執行可以使用
符合需求
再一次謝謝

另當〞List〞之H6以下欄位沒有資料時,則錯誤。
目前正研究如何排解,顯示通知並離開程式。
作者: Hsieh    時間: 2012-10-1 22:35

回復 4# b9208
  1. Sub ex()
  2. Set dic = CreateObject("Scripting.Dictionary")
  3. Set dic1 = CreateObject("Scripting.Dictionary")
  4. Set dic2 = CreateObject("Scripting.Dictionary")

  5. With Sheets("List")
  6. If .[H6].End(xlDown).Row = .Rows.Count Then MsgBox "無資料": Exit Sub  '無資料
  7. For Each a In .Range(.[H6], .[H65536].End(xlUp)).SpecialCells(xlCellTypeConstants)
  8. ar = Split(a, "+")
  9.    For Each c In ar
  10.       mystr = c & "," & Left(a.Offset(, 3), 8) & "," & a.Offset(, 8)
  11.       dic1(mystr) = Split(mystr, ",")
  12.    Next
  13. Next
  14. End With
  15. With Sheets("明細")
  16. ay = Application.Transpose(Application.Transpose(dic1.items))
  17. For i = 1 To UBound(ay, 1)
  18.    mystr = ay(i, 1) & ay(i, 2)
  19.    dic2(ay(i, 1)) = dic2(ay(i, 1)) + 1
  20.    If IsEmpty(dic(mystr)) Then
  21.      ary = Array(ay(i, 1), ay(i, 2), 1)
  22.    Else
  23.      ary = dic(mystr)
  24.      ary(2) = ary(2) + 1
  25.    End If
  26.    dic(mystr) = ary
  27. Next
  28. With .[B3].Resize(dic.Count, 3)
  29. .Value = Application.Transpose(Application.Transpose(dic.items))
  30. .Sort key1:=.Cells(1, 1), Header:=xlNo
  31. For Each a In .Columns(1).Cells
  32. If a <> a.Offset(-1, 0) Then a.Offset(, 3) = dic2(a.Value)
  33. Next
  34. End With
  35. End With
  36. End Sub
複製代碼

作者: b9208    時間: 2012-10-3 11:32

回復 5# Hsieh
Dear Hsieh,
非常感謝執行都ok
因資料筆數很多,所以查看SubTotal 資料不方便,
今想於〞明細〞工作表內[I3]輸出〞物品單項〞及[J3]輸出〞SubTotal〞。
懇請指導
謝謝
作者: Hsieh    時間: 2012-10-3 17:54

回復 6# b9208
  1. Sub ex()
  2. Set dic = CreateObject("Scripting.Dictionary")
  3. Set dic1 = CreateObject("Scripting.Dictionary")
  4. Set dic2 = CreateObject("Scripting.Dictionary")

  5. With Sheets("List")
  6. If .[H6].End(xlDown).Row = .Rows.Count Then MsgBox "無資料": Exit Sub  '無資料
  7. For Each a In .Range(.[H6], .[H65536].End(xlUp)).SpecialCells(xlCellTypeConstants)
  8. ar = Split(a, "+")
  9.    For Each c In ar
  10.       mystr = c & "," & Left(a.Offset(, 3), 8) & "," & a.Offset(, 8)
  11.       dic1(mystr) = Split(mystr, ",")
  12.    Next
  13. Next
  14. End With
  15. With Sheets("明細")
  16. ay = Application.Transpose(Application.Transpose(dic1.items))
  17. For i = 1 To UBound(ay, 1)
  18.    mystr = ay(i, 1) & ay(i, 2)
  19.    dic2(ay(i, 1)) = dic2(ay(i, 1)) + 1
  20.    If IsEmpty(dic(mystr)) Then
  21.      ary = Array(ay(i, 1), ay(i, 2), 1)
  22.    Else
  23.      ary = dic(mystr)
  24.      ary(2) = ary(2) + 1
  25.    End If
  26.    dic(mystr) = ary
  27. Next
  28. With .[B3].Resize(dic.Count, 3)
  29. .Value = Application.Transpose(Application.Transpose(dic.items))
  30. .Sort key1:=.Cells(1, 1), Header:=xlNo
  31. For Each a In .Columns(1).Cells
  32. If a <> a.Offset(-1, 0) Then a.Offset(, 3) = dic2(a.Value)
  33. Next
  34. End With
  35. .[I3].Resize(dic2.Count, 1) = Application.Transpose(dic2.keys)
  36. .[J3].Resize(dic2.Count, 1) = Application.Transpose(dic2.items)
  37. End With
  38. End Sub
複製代碼

作者: b9208    時間: 2014-2-8 15:04

回復 7# Hsieh
Dear Hsieh
增加篩選條件:List工作表C欄「單位」,符合明細工作表A 2 儲存格單位,才統計輸出資料。
如附件內紅色標示區
非常感謝指導
[attach]17452[/attach]




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)