fork download
  1. Option Explicit
  2.  
  3. Public Sub CSV_重複件数()
  4. Dim sh_arr As Variant 'シート名配列
  5. Dim col_arr As Variant 'カラム名配列
  6. sh_arr = Array("CSV")
  7. col_arr = Array("AK|AL", "AN|AO", "AQ|AR")
  8. Call Count_Dup(sh_arr, col_arr)
  9. End Sub
  10. Public Sub 重複_重複件数()
  11. Dim sh_arr As Variant 'シート名配列
  12. Dim col_arr As Variant 'カラム名配列
  13. sh_arr = Array("重複①", "重複②")
  14. col_arr = Array("G|I")
  15. Call Count_Dup(sh_arr, col_arr)
  16. End Sub
  17. '重複件数設定処理
  18. Private Sub Count_Dup(sh_arr As Variant, col_arr As Variant)
  19. Dim i As Long
  20. Dim j As Long
  21. Dim ws As Worksheet
  22. Dim col As Variant
  23. Dim t1 As Double
  24. Dim t2 As Double
  25. Dim dict As Object
  26.  
  27. t1 = Timer
  28. Set dict = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  29. Application.ScreenUpdating = False
  30. 'カラムの組み合わせ分繰り返す
  31. For i = 0 To UBound(col_arr)
  32. '参照カラムと出力カラムに分ける
  33. col = Split(col_arr(i), "|")
  34. 'dictionaryの初期化
  35. dict.RemoveAll
  36. 'シートの数分繰り返す
  37. For j = 0 To UBound(sh_arr)
  38. 'シートを設定し、該当シートの該当列の重複をdictionaryに登録する
  39. Set ws = Worksheets(sh_arr(j))
  40. Call set_key(ws, dict, col(0))
  41. Next
  42. 'シートの数分繰り返す
  43. For j = 0 To UBound(sh_arr)
  44. 'シートを設定し、該当シートの該当列へ重複件数を出力する
  45. Set ws = Worksheets(sh_arr(j))
  46. Call set_dup(ws, dict, col(0), col(1))
  47. Next
  48. Next
  49. Application.ScreenUpdating = True
  50. t2 = Timer
  51. MsgBox ("完了 処理時間=" & Int(t2 - t1) & "秒")
  52. End Sub
  53. '重複件数のdictionary登録
  54. Private Sub set_key(ws As Worksheet, dict As Object, ByVal col1 As String)
  55. Dim maxrow As Long
  56. Dim arr As Variant
  57. Dim i As Long
  58. Dim key As Variant
  59. '指定列の最大行数取得
  60. maxrow = ws.Cells(Rows.Count, col1).End(xlUp).Row
  61. 'メモリ配列へ転送
  62. arr = ws.Range(col1 & "2:" & col1 & maxrow).Value
  63. '2行目~最終行まで繰り返す
  64. For i = 1 To UBound(arr, 1)
  65. 'キー取得
  66. key = LCase(arr(i, 1))
  67. If key <> "" Then
  68. 'キーが空白でないなら件数をカウント
  69. If dict.exists(key) = False Then
  70. dict(key) = 1
  71. Else
  72. dict(key) = dict(key) + 1
  73. End If
  74. End If
  75. Next
  76. End Sub
  77. '重複件数の出力
  78. Private Sub set_dup(ws As Worksheet, dict As Object, ByVal col1 As String, ByVal col2 As String)
  79. Dim maxrow As Long
  80. Dim arr As Variant
  81. Dim arr2 As Variant
  82. Dim i As Long
  83. Dim key As Variant
  84. '指定列の最大行数取得
  85. maxrow = ws.Cells(Rows.Count, col1).End(xlUp).Row
  86. 'メモリ配列へ転送(キーの列と出力列の2つ)
  87. arr = ws.Range(col1 & "2:" & col1 & maxrow).Value
  88. arr2 = ws.Range(col2 & "2:" & col2 & maxrow).Value
  89. '2行目~最終行まで繰り返す
  90. For i = 1 To UBound(arr, 1)
  91. key = LCase(arr(i, 1))
  92. 'キーが空白なら空白を出力、キーが空白でないなら重複件数を出力
  93. If key = "" Then
  94. arr2(i, 1) = ""
  95. Else
  96. arr2(i, 1) = dict(key)
  97. End If
  98. Next
  99. ws.Range(col2 & "2:" & col2 & maxrow).Value = arr2
  100. End Sub
  101.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty