全シートを対象に値が含まれる行をコピー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
Sub Initialize() Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False Application.Calculation = xlCalculationManual ' 関数利用注意 End Sub Sub Finalize() Application.Calculation = xlCalculationAutomatic Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub Sub test1() Initialize '書き込みシート名 writeSheet = "工番集計" ' 1: 10列目に対象を含むか, 2: 11列目が日付として対象と一致するか。 ifValue = 2 ' 最終行が1万より多い場合終了 For Each w In Sheets If w.Cells.SpecialCells(xlLastCell).Row > 10000 Then MsgBox w.Name & "の行数が多いため終了します" Exit Sub End If Next w ' 検索文字列入力 nm = InputBox("値を入力してください") If nm = "" Then Exit Sub If ifValue = 2 And False = IsDate(nm) Then Exit Sub ' 書き込みシート準備 For Each w In Sheets If w.Name = writeSheet Then w.Delete Exit For End If Next w Sheets.Add After:=Sheets(1) ActiveSheet.Name = writeSheet currentRowNumber = 1 ' 探索処理 Dim rg As Range For Each w In Sheets bt = w.Cells.SpecialCells(xlLastCell).Row If bt <= 1 Then GoTo continue: Set rg = w.Range("A1:AA" & bt) For r = 1 To rg.Rows.Count Select Case ifValue Case 1 If InStr(rg.Cells(r, 10).Value, nm) > 0 Then Sheets(writeSheet).Range("A" & currentRowNumber & ":AA" & currentRowNumber).Value = rg.Range("A" & r & ":AA" & r).Value currentRowNumber = currentRowNumber + 1 End If Case 2 If IsDate(rg.Cells(r, 11).Value) Then ' 日付に変換可能 If CDate(rg.Cells(r, 11).Value) = CDate(nm) Then Sheets(writeSheet).Range("A" & currentRowNumber & ":AA" & currentRowNumber).Value = rg.Range("A" & r & ":AA" & r).Value currentRowNumber = currentRowNumber + 1 End If End If End Select Next continue: Next w Sheets(writeSheet).Activate Sheets(writeSheet).Columns.AutoFit Finalize End Sub |