Sub OneInstance01()
Dim dstSheet As Worksheet
Dim Ns As Worksheet
Dim mPath As String
Dim buf As String
Dim i As Long
Dim y As Long
Dim srcBook As Workbook
Dim Base
Dim Namae As String
Dim Sd As Date
Dim Ed As Date
Dim WF As Object
Set WF = WorksheetFunction
Set dstSheet = ThisWorkbook.Worksheets("抽出条件")
If Not Evaluate("=ISREF(抽出先シート!A1)") Then Sheets.Add.Name = "抽出先シート"
Set Ns = Worksheets("抽出先シート")
Ns.UsedRange.Clear
mPath = ThisWorkbook.Path & "\"
buf = Dir(mPath & "*.xls*")
y = 2
With dstSheet
Namae = .Range("B4").Value
Sd = .Range("B6").Value
Ed = .Range("B7").Value
End With
Do While buf <> ""
If buf <> ThisWorkbook.Name Then
Set srcBook = Workbooks.Open(mPath + buf)
Base = Intersect(srcBook.Worksheets("Sheet1").UsedRange, srcBook.Worksheets("Sheet1").Range("A:J"))
For i = 1 To UBound(Base, 1)
If Base(i, 1) = Namae And Base(i, 3) >= Sd And Base(i, 3) <= Ed Then
Ns.Cells(y, 2).Resize(, UBound(WF.Index(Base, i, 0))) = WF.Index(Base, i, 0)
y = y + 1
End If
DoEvents
Next
srcBook.Close False
End If
buf = Dir()
'Erase Base
Loop
Set dstSheet = Nothing
Set srcBook = Nothing
Set Ns = Nothing
Set WF = Nothing
End Sub
1Sub OneInstance01()
2 Dim dstSheet As Worksheet
3 Dim Ns As Worksheet
4 Dim mPath As String
5 Dim buf As String
6 Dim i As Long
7 Dim j As Integer
8 Dim y As Long
9 Dim srcBook As Workbook
10 Dim Base As Variant
11 Dim Namae As String
12 Dim Sd As Date
13 Dim Ed As Date
14 Dim Included As Variant '//[追加]抽出項目用
15 Dim NotIncluded As Variant '//[追加]抽出外項目用
16 Dim WF As Object
17 Set WF = WorksheetFunction
18 Set dstSheet = ThisWorkbook.Worksheets("抽出条件")
19 'If Not Evaluate("=ISREF(抽出先シート!A1)") Then Sheets.Add.Name = "抽出先シート" '//[削除]【回答修正】
20 'Set Ns = Worksheets("抽出先シート") '//[削除]
21 '//F4で指定したシートがない場合は作成【回答修正】
22 If Not Evaluate("=ISREF(" + dstSheet.Range("F4").Value + "!A1)") Then Sheets.Add.Name = dstSheet.Range("F4").Value '//[追加]
23 Set Ns = Worksheets(dstSheet.Range("F4").Value) '//[追加]抽出先シートはセルF4の値に基づく
2425 Ns.UsedRange.Clear '//抽出先シートのデータを削除
26 mPath = ThisWorkbook.Path & "\" '//マクロ保存しているファイルのフォルダパス
27 buf = Dir(mPath & "*.xls*") '//フォルダ内のxlsファイル名をひとつ取得(マクロファイルも含む)
28 y = 2
29 With dstSheet
30 Namae = .Range("B4").Value '//商品名
31 Sd = .Range("B6").Value '//以降
32 Ed = .Range("B7").Value '//以前
33 Included = WorksheetFunction.Transpose(.Range("F6:F10")) '//[追加]抽出対象項目を配列化
34 NotIncluded = WorksheetFunction.Transpose(.Range("F12:F16")) '//[追加]抽出対象外項目を配列化
35 End With
36 Do While buf <> "" '//取得したファイルをひとつずつ実行
37 If buf <> ThisWorkbook.Name Then '//マクロファイルは処理しない
38 Set srcBook = Workbooks.Open(mPath + buf) '//処理対象のワークブック
39 Base = Intersect(srcBook.Worksheets("Sheet1").UsedRange, srcBook.Worksheets("Sheet1").Range("A:J")) '//値が存在する範囲を2次元配列化
40 For i = 1 To UBound(Base, 1) '//Base配列の1次元インデックスの最大まで
41 If Base(i, 1) = Namae And Base(i, 3) >= Sd And Base(i, 3) <= Ed And ItemInc(Included, NotIncluded, Base(i, 4)) Then '//[変更]
42 '//(右辺)WF.Index(Base, i, 0) -> Base(2次元配列)のi行目を配列で取得(第3引数が0だから).
43 '//(左辺).Resize(, UBound(WF.Index(Base, i, 0))) -> Cells(y,2)を列方向に10列分拡張.
44 Ns.Cells(y, 2).Resize(, UBound(WF.Index(Base, i, 0))) = WF.Index(Base, i, 0)
45 y = y + 1
46 End If
47 DoEvents
48 Next
49 srcBook.Close False '//ワークブックを閉じる
50 End If
51 buf = Dir() '//次のxlsxファイルを取得(全て処理したら、while文終了)
52 'Erase Base
53 Loop
54 Set dstSheet = Nothing
55 Set srcBook = Nothing
56 Set Ns = Nothing
57 Set WF = Nothing
58End Sub
5960'//[追加]項目が抽出対象ならTRUE、削除対象ならFALSE
61Function ItemInc(Included As Variant, NotIncluded As Variant, Item As Variant) As Boolean
62 Dim Bool As Boolean '//戻り値
63 Dim Value As Variant
6465 '//項目が空ならFalse
66 If Item = Empty Then
67 ItemInc = False '//戻り値
68 Exit Function '//処理終了
69 End If
7071 If Join(Included, "") = "" And Join(NotIncluded, "") = "" Then
72 '//[抽出、削除]未設定なら全部True
73 ItemInc = True
74 Exit Function
75 ElseIf Join(Included, "") <> "" And Join(NotIncluded, "") = "" Then
76 '//[抽出]の項目のみ設定されている場合
77 Bool = False
78 For Each Value In Included
79 If Value = Item Then
80 Bool = True
81 End If
82 Next Value
83 ElseIf Join(Included, "") = "" And Join(NotIncluded, "") <> "" Then
84 '//[削除]の項目のみ設定されている場合
85 Bool = True
86 For Each Value In NotIncluded
87 If Value = Item Then
88 Bool = False
89 End If
90 Next Value
91 Else
92 '//[抽出、削除]の両方に条件設定されている場合
93 '//[抽出]項目のみの設定と同じ
94 Bool = False
95 For Each Value In Included
96 If Value = Item Then
97 Bool = True
98 End If
99 Next Value
100 End If
101102 ItemInc = Bool '//戻り値
103104End Function