fork download
  1. Option Explicit
  2.  
  3. Sub SearchAcrossBooks_OnePass()
  4. Dim wsMain As Worksheet
  5. Dim lastrow As Long, r As Long, endRow As Long
  6. Dim dict As Object
  7. Dim key As Variant, v As Variant
  8. Dim Path As String, f As String
  9. Dim wb As Workbook, ws As Worksheet
  10. Dim rng As Range
  11. Dim data As Variant
  12. Dim i As Long, j As Long
  13. Dim sc As Boolean, ev As Boolean, calc As XlCalculation
  14. Dim cnt As Long
  15. Dim FDL As FileDialog
  16. Dim selFolder As String
  17.  
  18. ' フォルダ選択ダイアログを作成
  19. Set FDL = Application.FileDialog(msoFileDialogFolderPicker)
  20.  
  21. ' ダイアログの初期フォルダ設定(オプション)
  22. 'FDL.InitialFileName = "C:\"
  23.  
  24. ' ダイアログ表示し、フォルダが選択されたか確認
  25. If FDL.Show = -1 Then
  26. ' 選択されたフォルダパスを取得
  27. selFolder = FDL.SelectedItems(1)
  28. Else
  29. 'フォルダ選択がキャンセルの場合
  30. Exit Sub
  31. End If
  32.  
  33. Set wsMain = ThisWorkbook.Sheets("判定")
  34.  
  35. lastrow = wsMain.Cells(wsMain.Rows.Count, "E").End(xlUp).Row
  36. If lastrow < 2 Then Exit Sub
  37.  
  38. Set dict = CreateObject("Scripting.Dictionary")
  39.  
  40. For r = 2 To lastrow
  41. v = wsMain.Cells(r, "E").Value
  42. If Len(v) = 0 Then Exit For
  43. dict(CStr(v)) = 0
  44. Next r
  45. endRow = r - 1
  46. If dict.Count = 0 Then Exit Sub
  47.  
  48. sc = Application.ScreenUpdating
  49. ev = Application.EnableEvents
  50. calc = Application.Calculation
  51. Application.ScreenUpdating = False
  52. Application.EnableEvents = False
  53. Application.Calculation = xlCalculationManual
  54.  
  55.  
  56.  
  57. ' 同一フォルダ内の他ブックを集計
  58. Path = selFolder & "\"
  59. f = Dir(Path & "*.xls*")
  60. Do While f <> ""
  61. If Path & f <> ThisWorkbook.FullName Then
  62. On Error Resume Next
  63. Set wb = Workbooks.Open(Path & f, ReadOnly:=True)
  64. If Err.Number = 0 Then
  65. On Error GoTo 0
  66. Call CountInWorkbook(wb, dict)
  67. wb.Close SaveChanges:=False
  68. Else
  69. Err.Clear
  70. On Error GoTo 0
  71. End If
  72. End If
  73. f = Dir()
  74. Loop
  75.  
  76. ' 結果を書き戻し
  77. For r = 2 To endRow
  78. cnt = dict(CStr(wsMain.Cells(r, "E").Value))
  79. Select Case cnt
  80. Case 0: wsMain.Cells(r, "A").Value = "×"
  81. Case 1: wsMain.Cells(r, "A").Value = "◯"
  82. Case Else: wsMain.Cells(r, "A").Value = "△"
  83. End Select
  84. Next r
  85.  
  86. Application.ScreenUpdating = sc
  87. Application.EnableEvents = ev
  88. Application.Calculation = calc
  89.  
  90. End Sub
  91.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty