返回列表 上一主題 發帖

[發問] 依篩選條件輸出明細並統計

[發問] 依篩選條件輸出明細並統計

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

B1.rar (9.01 KB)
100 字節以內
不支持自定義 Discuz! 代碼

回復 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
複製代碼
學海無涯_不恥下問

TOP

回復 2# Hsieh


    謝謝 好用的想法 和解答
    拾起 以後有機會可以用...
50 字節以內
不支持自定義 Discuz! 代碼

TOP

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

另當〞List〞之H6以下欄位沒有資料時,則錯誤。
目前正研究如何排解,顯示通知並離開程式。
100 字節以內
不支持自定義 Discuz! 代碼

TOP

回復 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
複製代碼
學海無涯_不恥下問

TOP

回復 5# Hsieh
Dear Hsieh,
非常感謝執行都ok
因資料筆數很多,所以查看SubTotal 資料不方便,
今想於〞明細〞工作表內[I3]輸出〞物品單項〞及[J3]輸出〞SubTotal〞。
懇請指導
謝謝
100 字節以內
不支持自定義 Discuz! 代碼

TOP

回復 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
複製代碼
學海無涯_不恥下問

TOP

回復 7# Hsieh
Dear Hsieh
增加篩選條件:List工作表C欄「單位」,符合明細工作表A 2 儲存格單位,才統計輸出資料。
如附件內紅色標示區
非常感謝指導
B2.rar (8.14 KB)
100 字節以內
不支持自定義 Discuz! 代碼

TOP

        靜思自在 : 君子立恆志,小人恆立志。
返回列表 上一主題