セルB1にPath,セルB2にWildcardが入るという前提です。
'---------------------------------------------------------
Option Explicit
'フォルダ名を取得する関数(SHBrowseForFolder)
Private Type BROWSEINFO
hwndOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList
Lib "shell32.dll" Alias "SHGetPathFromIDListA"
( _
ByVal PointerToIdList As Long, ByVal pszPath
As String) As Long
Private Declare Function SHBrowseForFolder
Lib "shell32.dll" Alias "SHBrowseForFolderA"
(lpBROWSEINFO As BROWSEINFO) As Long
Private Declare Function CoTaskMemFree Lib
"ole32.dll" (ByVal pv As Long)
As Long
Private Declare Function FindWindow Lib "user32"
Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName
As String) As Long
Private Const CSIDL_DESKTOP As Long = 0
Private Const BIF_RETURNONLYFSDIRS As Long
= &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long
= &H2
Private Const MAX_PATH = 260
'フォルダ名を取得する関数(SHBrowseForFolder)
Function GetFolderName(ByVal hwnd As Long,
_
ByVal sPrompt As String, ByRef sPath As String)
As Long
Dim bi As BROWSEINFO
Dim pidl As Long
Dim iRet As Long
GetFolderName = -1
On Error GoTo ErrorHandler
bi.hwndOwner = hwnd
bi.pidlRoot = CSIDL_DESKTOP
bi.pszDisplayName = String$(MAX_PATH + 1,
Chr$(10))
bi.lpszTitle = sPrompt
bi.ulFlags = BIF_RETURNONLYFSDIRS
bi.lpfn = 0
bi.lParam = 0
bi.iImage = 0
pidl = SHBrowseForFolder(bi)
If pidl = 0 Then
GetFolderName = 1
Exit Function
End If
sPath = String$(MAX_PATH + 1, Chr$(0))
If SHGetPathFromIDList(ByVal pidl, ByVal
sPath) = 0 Then
iRet = CoTaskMemFree(pidl)
Exit Function
End If
sPath = Left(sPath, InStr(sPath, Chr$(0))
- 1)
iRet = CoTaskMemFree(pidl)
If iRet <> 0 Then GetFolderName = 0
Exit Function
ErrorHandler:
If pidl <> 0 Then CoTaskMemFree pidl
End Function
Sub READ_FILELIST()
Dim DataFolderName As String
Dim DataFileName As String
Dim DataFileTime As String
Dim WildCard As String
Dim ActiveRow As Integer, ActiveColumn As
Integer
Dim FileNum As Integer
DataFolderName = Cells(1, 2).Value
WildCard = Cells(2, 2).Value
ActiveRow = 5
ActiveColumn = 1
FileNum = 1
Range("A4:C65536").Select
Selection.ClearContents
Range("A4").Select
DataFileName = Dir$(DataFolderName &
WildCard)
Cells(ActiveRow, ActiveColumn + 1) = DataFileName
If DataFileName = "" Then Exit
Sub
Cells(ActiveRow, ActiveColumn) = FileNum
DataFileTime = FileDateTime(DataFolderName
& DataFileName)
Cells(ActiveRow, ActiveColumn + 2) = DataFileTime
ActiveRow = ActiveRow + 1
FileNum = FileNum + 1
Do
DataFileName = Dir$
Cells(ActiveRow, ActiveColumn + 1) = DataFileName
If DataFileName = "" Then Exit
Sub
Cells(ActiveRow, ActiveColumn) = FileNum
DataFileTime = FileDateTime(DataFolderName
& DataFileName)
Cells(ActiveRow, ActiveColumn + 2) = DataFileTime
ActiveRow = ActiveRow + 1
FileNum = FileNum + 1
Loop
End Sub
'---------------------------------------------------------
Function GetFolderNameはWindowsの標準ダイアログを利用してフォルダ名を取得する関数です。
Sub READ_FILELISTはDir関数によりファイル名を取得しています。
Dir[(pathname[, attributes])]
指定したパターンやファイル属性と一致するファイルまたはフォルダの名前を表す文字列型 (String) の値を返します。ドライブのボリューム ラベルも取得できます。
フォルダ内のすべてのファイルに対して繰り返して処理を実行する場合は、引数を指定せずにDir を実行してください。