fork download
  1. Option Explicit
  2. Public Sub 集計()
  3. Dim dicT1 As Object 'キー:担当者 値:Sheet2の行番号
  4. Dim dicT2 As Object 'キー:担当者+日 値:True
  5. Dim lrow1 As Long 'Sheet1 最終行
  6. Dim lrow2 As Long: lrow2 = 3 'Sheet2  最終行
  7. Dim key1 As String
  8. Dim key2 As String
  9. Dim sh1 As Worksheet 'Sheet1
  10. Dim sh2 As Worksheet 'Sheet2
  11. Dim row1 As Long
  12. Dim row2 As Long
  13. Set dicT1 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  14. Set dicT2 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  15. Set sh1 = Worksheets("sheet1")
  16. Set sh2 = Worksheets("sheet2")
  17. sh2.Rows("3:" & Rows.Count).ClearContents 'Sheet2 3行以降をクリア
  18. lrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).Row 'Sheet1 最終行を求める
  19. For row1 = 2 To lrow1
  20. key1 = sh1.Cells(row1, "L").Value 'キー:担当者
  21. If dicT1.exists(key1) = False Then
  22. dicT1(key1) = lrow2
  23. sh2.Cells(lrow2, "B").Value = sh1.Cells(row1, "L").Value '担当者設定
  24. lrow2 = lrow2 + 1
  25. End If
  26. row2 = dicT1(key1)
  27. sh2.Cells(row2, "D").Value = sh2.Cells(row2, "D").Value + sh1.Cells(row1, "K").Value '距離加算
  28. key2 = sh1.Cells(row1, "L").Value & "|" & sh1.Cells(row1, "B").Value 'キー:担当者+日
  29. '当該日の最初の1回目のみ1加算する
  30. If dicT2.exists(key2) = False Then
  31. sh2.Cells(row2, "C").Value = sh2.Cells(row2, "C").Value + 1 '日数へ1加算
  32. dicT2(key2) = True
  33. End If
  34. Next
  35. MsgBox ("完了")
  36. End Sub
  37.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty