|
もし、「Voiding」のカウント一覧表みたいな物が必要なら
2つのプロシージャを以下の様に変更して下さい
Public Sub ReadRle()
Dim i As Long
Dim vntFileNames As Variant
Dim lngWrite As Long
Dim blnHeader As Boolean
Dim wkbNewBook As Workbook
Dim wksNewSheet As Worksheet
Dim wksVoiding As Worksheet
Dim vntVoiding As Variant
Dim lngVoidingRow As Long
Dim strPath As String
Dim objFso As Object
'FSOのオブジェクトを取得
Set objFso = CreateObject("Scripting.FileSystemObject")
' Application.ScreenUpdating = False
'新規Bookを追加
Set wkbNewBook = Workbooks.Add
Set wksVoiding = wkbNewBook.Worksheets("Sheet1")
lngVoidingRow = 1
wksVoiding.Cells(lngVoidingRow, "A").Resize(, 4).Value _
= Array("Folder", "FileName", "Voiding", "Voiding累計")
lngVoidingRow = lngVoidingRow + 1
'Voidingカウント用の変数を初期化
ReDim vntVoiding(1 To 3)
'ファイルを開くダイアログを表示してファイル名を取得
Do While GetReadFile(vntFileNames, ThisWorkbook.Path, True)
'新規Bookに書き込み用シートを追加
With wkbNewBook.Worksheets
Set wksNewSheet = .Add(After:=.Item(.Count))
End With
strPath = objFso.GetParentFolderName(vntFileNames(1))
wksVoiding.Cells(lngVoidingRow, 1).Value = strPath
'書き込み行の初期値
lngWrite = 1
'先頭行を書き込み
blnHeader = True
'取得したファイル名全てに繰り返し
For i = 1 To UBound(vntFileNames)
'Voidingカウント用の変数を初期設定
vntVoiding(1) = objFso.GetBaseName(vntFileNames(i))
vntVoiding(2) = 0
'書き込み用シートに出力
CSVRead vntFileNames(i), objFso, wksNewSheet, _
vntVoiding, lngWrite, 1, blnHeader, ","
'先頭行をスッキプ
'blnHeader = False
'Voidingカウント総数用の変数にVoidingカウントを加算
vntVoiding(3) = vntVoiding(3) + vntVoiding(2)
With wksVoiding
.Cells(lngVoidingRow, 2).Resize(, 3).Value = vntVoiding
lngVoidingRow = lngVoidingRow + 1
End With
Next i
wksNewSheet.Name = NameLetter(strPath)
'ファイル名取得用変数の初期化
vntFileNames = ""
Loop
Set wksNewSheet = Nothing
Set wkbNewBook = Nothing
Set wksVoiding = Nothing
Set objFso = Nothing
' Application.ScreenUpdating = True
Beep
MsgBox "処理が完了しました"
End Sub
Private Sub CSVRead(ByVal strFileName As String, _
ByVal objFso As Object, _
ByRef wksWrite As Worksheet, _
ByRef vntTotal As Variant, _
Optional ByRef lngRow As Long = 1, _
Optional ByRef lngCol As Long = 1, _
Optional ByRef blnHeader As Boolean = True, _
Optional strDelim As String = ",")
Dim vntField As Variant
Dim strLine As String
Dim blnMulti As Boolean
Dim strRec As String
Dim wkbParent As Workbook
Dim objFileStr As Object
Const ForReading = 1
Set wkbParent = wksWrite.Parent
'指定ファイルを読み込みモードでOpen
Set objFileStr = objFso.OpenTextFile(strFileName, ForReading)
With objFileStr
Do Until .AtEndOfStream
strLine = .ReadLine
strRec = strRec & strLine
vntField = SplitCsv(strRec, strDelim, , , blnMulti)
If blnMulti Then
strRec = strRec & vbLf
Else
If blnHeader Then
If vntField(4) = "Voiding" Then
vntTotal(2) = vntTotal(2) + 1
End If
With wksWrite.Cells(lngRow, lngCol)
.Offset.Resize(, UBound(vntField) + 1) = vntField
End With
lngRow = lngRow + 1
If lngRow > 65536 Then
lngRow = 1
With wkbParent.Worksheets
Set wksWrite = .Add(After:=.Item(.Count))
End With
End If
End If
strRec = ""
blnHeader = True
End If
Loop
.Close
End With
Set objFileStr = Nothing
Set wkbParent = Nothing
End Sub
'其れと、以下のプロシージャを追加して下さ
Private Function NameLetter(ByVal strName As String) As String
' シート名のチェック
Dim i As Long
Dim vntLetter As Variant
Dim lngPos As Long
'シート名として使用不可能な文字の一覧を作成
vntLetter = Array(":", "\", "?", "[", "]", "/", "*")
'一覧全てに就いて
For i = 0 To UBound(vntLetter, 1)
'引数の文字列に一覧の文字が含まれるか探索
lngPos = InStr(1, strName, vntLetter(i), vbTextCompare)
'引数の文字列に一覧の文字が無くなるまで繰り返し
Do Until lngPos = 0
'有る場合、"_"に置換
strName = Left(strName, lngPos - 1) _
& "_" & Mid(strName, lngPos + 1)
'引数の文字列に一覧の文字が含まれるか探索
lngPos = InStr(1, strName, vntLetter(i), vbTextCompare)
Loop
Next i
'戻り値として、置換後の文字列を返す
NameLetter = strName
End Function
|
|