PCを使っていない企業は無いと思うが、逆に管理が面倒になっていることはないだろうか? との「年寄りの心配」(ヒョッとして冷や水かも)から、FS社の例を参考に「指定のフォルダ下にあるファイルの一覧を作る」マクロ(VBA)を作ってみた。
今後参考にするために記録する...
§1.Decraretions
- Option Explicit
- Const TXTFileName As String = "FolderList.txt"
- Const COLPathName As Long = 5 'フォルダー名
- Const COLFileName As Long = 6 'ファイル名
- Dim BOOLAns As Boolean
- Dim LNGAns As Long
- Dim STRAns As String
- Dim VARAns As Variant
§2.Auto_Open
- Sub Auto_Open()
- Dim mbTitle As String
- Dim strTopPath As String
- mbTitle = "Auto_Open/" & ThisWorkbook.Name
- strTopPath = Worksheets("更新履歴").Range("B2")
- Worksheets("CADNo").Select
- Range("D1").Select
- VARAns = MsgBox("CAD No.の一覧を作成します。" & vbCrLf & vbCrLf _
- & " ■前回トップフォルダ=" & strTopPath & vbCrLf & vbCrLf _
- & "トップフォルダを変更する時は、[いいえ]を選択します。" _
- , vbYesNoCancel + vbExclamation, mbTitle)
- If VARAns = vbCancel Then Exit Sub
- If VARAns = vbNo Then strTopPath = ""
- If strTopPath = "" Then
- strTopPath = getフォルダ名()
- If strTopPath = "" Then Exit Sub
- End If
- 'トップフォルダーを保存する。
- Worksheets("更新履歴").Select
- Range("B1") = Now()
- Range("B2") = strTopPath
- BOOLAns = makeCADNoリスト(strTopPath)
- BOOLAns = 修整する()
- End Sub
§3.makeCADNoリスト
- Function makeCADNoリスト(argTopPath As String)
- Dim dsn As Long, i As Long
- Dim strTopPath As String, flnFolderList As String
- Dim strPath As String, strFileName As String
- flnFolderList = ThisWorkbook.Path & "\" & TXTFileName
- VARAns = makeフォルダーリスト(argTopPath, flnFolderList)
- Worksheets("CADNo").Select
- 'クリアする
- Range("A2").Select
- Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
- Selection.EntireRow.Delete
- Range("A2").Select
- 'フォルダーリストを読んでパスとファイル名を書き出す。
- dsn = FreeFile
- Open flnFolderList For Input As dsn
- i = 1
- Do Until EOF(dsn)
- Input #dsn, strPath
- '出力ファイル名を作る。
- If Right(strPath, 1) = "\" Then
- STRAns = strPath & "*.*"
- Else
- STRAns = strPath & "\*.*"
- End If
- strFileName = Dir(STRAns)
- Do Until strFileName = ""
- i = i + 1
- Cells(i, COLPathName) = strPath
- Cells(i, COLFileName) = strFileName
- strFileName = Dir()
- Loop
- Loop
- Close dsn
- End Function
§4.makeフォルダーリスト
- Function makeフォルダーリスト(argTopFolder As String, argFolderList As String)
- Dim dsn As Long
- dsn = FreeFile
- Open argFolderList For Output As dsn
- 'トップフォルダーを出力する
- Print #dsn, argTopFolder
- VARAns = get全フォルダ名(argTopFolder, dsn)
- Close dsn
- End Function
§5.get全フォルダ名
再帰呼び出し(recursive call)を使って、全フォルダ名(パス名)をテキストファイルに書き出す。
- Function get全フォルダ名(argPath As String, argDSN As Long)
- Dim mbTitle As String
- Dim obj As Object
- Dim strBuffer As String
- mbTitle = "get全フォルダ名/" & ThisWorkbook.Name
- With CreateObject("Scripting.FileSystemObject")
- For Each obj In .GetFolder(argPath).SubFolders
- VARAns = get全フォルダ名(obj.Path, argDSN)
- If Dir(obj.Path, vbDirectory) <> "" Then Print #argDSN, obj.Path
- Next obj
- End With
- End Function
§6.getフォルダ名
- Function getフォルダ名() As String
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "トップ フォルダーを指定してください。"
- If .Show = True Then
- getフォルダ名 = .SelectedItems(1)
- Else
- getフォルダ名 = ""
- End If
- End With
- End Function
§7.修整する
- Function 修整する() As Boolean
- Dim d As Long, i As Long, y As Long
- Dim lastRow As Long, lenTopPath As Long
- Dim strF1 As String, strF2 As String, strF3 As String
- Dim strTopPath As String, strString As String
- strTopPath = Worksheets("更新履歴").Range("B2")
- If Right(strTopPath, 1) = "\" Then strTopPath = Left(strTopPath, Len(strTopPath) - 1)
- lenTopPath = Len(strTopPath)
- lastRow = Cells(2, COLPathName).End(xlDown).Row
- '書式を文字列にする。
- Columns("A:D").Select
- Selection.NumberFormatLocal = "@"
- Range("A2").Select
- For i = 2 To lastRow
- strString = Cells(i, COLPathName)
- strString = Mid(strString, lenTopPath + 2)
- 'F1を作る。
- If strString = "" Then
- strF1 = ""
- Else
- y = InStr(strString, "\")
- If y = 0 Then
- strF1 = strString
- strString = ""
- Else
- strF1 = Left(strString, y - 1)
- strString = Mid(strString, y + 1)
- End If
- End If
- Cells(i, 1) = strF1
- 'F2を作る。
- If strString = "" Then
- strF2 = ""
- Else
- y = InStr(strString, "\")
- If y = 0 Then
- strF2 = strString
- strString = ""
- Else
- strF2 = Left(strString, y - 1)
- strString = Mid(strString, y + 1)
- End If
- End If
- Cells(i, 2) = strF2
- If strString = "" Then
- strF3 = ""
- Else
- y = InStr(strString, "\")
- If y = 0 Then
- strF3 = strString
- strString = ""
- Else
- strF3 = Left(strString, y - 1)
- strString = Mid(strString, y + 1)
- End If
- End If
- Cells(i, 3) = strF3
- strString = Cells(i, COLFileName)
- d = InStrRev(strString, ".")
- Cells(i, 4) = Left(strString, d - 1)
- Next
- 'CADNoを降順に並べる。
- Range("D1").Select
- ActiveWorkbook.Worksheets("CADNo").Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("CADNo").Sort.SortFields.Add Key:=Range("D2:D" & lastRow), _
- SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
- xlSortTextAsNumbers
- With ActiveWorkbook.Worksheets("CADNo").Sort
- .SetRange Range("A1:F" & lastRow)
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlStroke
- .Apply
- End With
- 修整する = True
- End Function