
VBAでフォルダを指定してエクセルファイル名を検索し、ファイル名のリストを作成しようとしています(Excel2010)。フォルダおよびファイルを連番で名づけた順に取得したいのですが、検索するフォルダが、01**,02**,03**・・・110**という連番で名づけられていた場合、検索順が、01,02,03,・・・09,110,111,112、・・となり、連番順に検索できません。1,2,3,・・・10,11・・と名付けると、1**からではなく10**から検索されてしまいます。
01,02,03**・・順で取得する方法と、1,2,3**・・の連番順で取得する方法を教えていただけるでしょうか。
よろしくお願いします。
Sub FileSearch(strPath As String)
Dim fso As Object
Dim strFolder As Variant
Dim strFile As Variant
Dim strPathExcel() As String
Dim l As Long
Set fso = CreateObject("Scripting.fileSystemObject")
For Each strFolder In fso.getfolder(strPath).subfolders
Call FileSearch(strFolder.Path)
Next strFolder
l = 0
For Each strFile In fso.getfolder(strPath).Files
If InStr(strFile.Type, "Excel") > 0 Then
ReDim Preserve strPathExcel(l)
strPathExcel(l) = strFile.Path
l = l + 1
End If
Next strFile
End Sub
No.1ベストアンサー
- 回答日時:
こんにちは。
エクセル VBAだと思いますが、シートに出して、そこで関数やソートなどと組合せて操作したほうが早いと思います。
もし、コードで行うなら、フォルダの中身自体をソートするのかは分かりませんが、まず、以下のようにフォルダとファイルを分離することでしょうね。そして、フォルダに、ソートアルゴリズムを通せばよいです。ただ、ややこしいし、今のところ、一つ下のフォルダーしか調べていません。
'//
Sub TestMacro1()
Dim fso As Object
Dim strPath As String
Dim strFolder As Variant
Dim i As Long, n As Long, j As Long, l As Long
Dim a()
Dim strFile As Variant
Dim strPathExcel() As String
Dim inFiles() As Variant
Dim varFol As Variant
Dim x As Long, y As Variant
Dim ar2 As Variant
'******ユーザー設定**********
Const blnTXT As Boolean = False 'フォルダのテキスト比較
strPath = "C:\Users\Temp\" 'ユーザーフォルダー
'****************************
'Cells.Clear 'シート全体を消す
i = 0
Set fso = CreateObject("Scripting.fileSystemObject")
For Each strFolder In fso.getfolder(strPath).subfolders
ReDim Preserve a(i)
a(i) = strFolder.Path
i = i + 1
Next strFolder
Call Bubble_Sort(a(), blnTXT) 'バブルソート
ReDim inFiles(UBound(a()), 1)
For Each varFol In a()
For Each strFile In fso.getfolder(varFol).Files
If InStr(strFile.Type, "Excel") > 0 Then
ReDim Preserve strPathExcel(l)
strPathExcel(l) = Dir(strFile.Path)
l = l + 1
End If
Next strFile
inFiles(n, 0) = varFol
inFiles(n, 1) = strPathExcel()
n = n + 1
l = 0
Erase strPathExcel
Next varFol
i = 1: j = 1
For x = 0 To UBound(inFiles)
Cells(i, 1).Value = inFiles(x, 0)
ar2 = inFiles(x, 1)
On Error Resume Next
y = Empty
y = UBound(ar2)
On Error GoTo 0
If Not IsEmpty(y) Then
Cells(j, 2).Resize(y + 1).Value = Application.Transpose(ar2)
j = i + UBound(ar2) + 1 + 1
i = j
End If
Next x
End Sub
Sub Bubble_Sort(ByRef a(), Optional txt As Boolean = False)
'txt オプションは、text 比較の意味
Dim u As Long
Dim i As Long
Dim j As Long
Dim t As Variant
Dim m As Variant, n As Variant
u = UBound(a())
i = LBound(a())
Do While i < u
j = u
Do While j > i
If txt Then
m = Dir(a(j), vbDirectory)
n = Dir(a(i), vbDirectory)
Else
m = Val(Dir(a(j), vbDirectory))
n = Val(Dir(a(i), vbDirectory))
End If
If m < n Then '判定 <昇順
t = a(i)
a(i) = a(j)
a(j) = t
End If
j = j - 1
Loop
i = i + 1
Loop
End Sub
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
Windows10でコマンドプロンプト...
-
ディレクトリ名変更してコピー...
-
クラウドにあるフォルダを共有...
-
【ExcelVBA】一覧表の記載に従...
-
vbsで選択ダイアログを表示した...
-
VBScriptで空フォルダ圧縮
-
ExcelのVBAでの複数階層からの...
-
【VBS】古い日付のフォルダを削...
-
エクセルのデータをメモ帳に貼...
-
Access VBA で フォルダ権限...
-
Excel VBA マクロ リストボックス
-
サーバ内のフォルダ名と各フォ...
-
Downloaded Program Filesはど...
-
FileSystemObjectでのパス名の取得
-
Debug フォルダは消していいの?
-
あるフォルダの中にあるファイ...
-
C++Builder Ver6.0.でコンポー...
-
パス名に2バイト文字(マルチバ...
-
「フォルダの参照」ダイアログ...
-
Excelのハイパーリンクについて...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Windows10でコマンドプロンプト...
-
会社のネットワーク上のファイ...
-
ファイル名と同名のフォルダを...
-
VBA フォルダ名に特定の文字を...
-
ExcelのVBAでフォルダ指定がで...
-
デスクトップの画像をhtmlに表...
-
VBA 最新のフォルダ取得
-
VBA フォルダの複数選択ができない
-
Excelのハイパーリンクについて...
-
パス名に2バイト文字(マルチバ...
-
【コマンドプロンプト】名前順...
-
【ExcelVBA】一覧表の記載に従...
-
サーバ内のフォルダ名と各フォ...
-
Wallpaper Engineでおすすめの...
-
ファイルとフォルダのどちらも...
-
Debug フォルダは消していいの?
-
Excelで指定したフォルダに保存...
-
VBプロジェクトでのフォルダ構...
-
GetAttrが原因?
-
Hitachi Embedded Workshop (HE...
おすすめ情報