作成したマクロのコードは下記です。
Option Explicit
'-----ファイル処理-----'
Sub appendFile(path As String, fname As String, outfname As String)
Dim buf As String, idx As String
Dim fname2 As String, fname3 As String
Dim x As Long
'-----CSVファイルの読み込みと書き込みを実行-----'
fname2 = path & fname
Open fname2 For Input As #1
Open outfname For Append As #2
Do Until EOF(1)
Line Input #1, buf
Print #2, buf
Loop
Close #2
Close #1
End Sub
'-----Sub mainにあるhogeCouplingからディレクトリ、ファイル形式、出力形式を呼び出し-----'
Sub hogeCoupling(path As String, ext As String, outfname As String)
Dim fcol As Object, re As Object
Dim flist As Variant, remat As Variant
Dim pat As String, fname As String
Dim n As Long
'-----処理対象ファイル探索と処理の実行-----'
Set fcol = CreateObject("Scripting.FileSystemObject").GetFolder(path).Files
Set re = CreateObject("VBScript.RegExp")
pat = "^" & Cells(11, 4) '入力した2つのセルを組み合わせた形式のファイルを定義
With re
.Pattern = pat
.IgnoreCase = True
.Global = True
For Each flist In fcol
Set remat = .Execute(flist.Name)
If remat.Count > 0 Then
Call appendFile(path, flist.Name, outfname)
End If
Next flist
End With
Set re = Nothing
Set fcol = Nothing
End Sub
Sub main()
Call hogeCoupling("C:/test/Log/", "csv", "C:/test/出力結果/" & Cells(11, 4) & "_Summarize.csv")
MsgBox "作成完了しました、ファイルを確認してください"
End Sub
▽1
●
Silvanus ベストアンサー |
これで行けると思うのですが、いかがでしょうか。
既存コードのSub main()中にある"Call hogeCoupling(..."の次行に
SortByTime "C:/test/出力結果/" & Cells(11, 4) & "_Summarize.csv"
を記入した後に下記のサブルーティンをどこかに追加して下さい。
Sub SortByTime(ByVal strCSVFile as string) Dim rngLstCll As Range Workbooks.Open Filename:=strCSVFile Set rngLstCll = Cells.SpecialCells(xlCellTypeLastCell) With ActiveSheet.Sort .SortFields.Clear .SortFields.Add Key:=Range(Cells(1, 1), Cells(rngLstCll.Row, 1)), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange Range(Cells(1, 1), rngLstCll) .MatchCase = False .Orientation = xlTopToBottom .Apply End With Set rngLstCll = Nothing ActiveWorkbook.Save Application.DisplayAlerts = False ActiveWindow.Close Application.DisplayAlerts = True End Sub