Excel VBA 質問スレ Part83 (632レス)
前次1-
抽出解除 レス栞

リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
417
(3): 07/17(木)08:47 ID:sGHKyXGW(1)調 AAS
>>410
Dirでワイルドカードが使えるので、こんな感じに仕上げてみた
※ヒットするパスの例 → C:\Documents\A001\B000X1\Excel\20250717\FileABC001.xlsx

HomePath = "C:\Documents"
SearchPath = "A???\B*\Excel\2025????\File*.xlsx"

ResultPath = HomePath
SearchPathList = Split(SearchPath, "\")
For I = 0 To UBound(SearchPathList)
 If Dir(ResultPath) <> "" Then
  ResultPath = "ファイルの下は何もないよ!"
  Exit For
 End If
 FolderName = Dir(ResultPath & "\" & SearchPathList(I), vbDirectory)
 If FolderName = "" Then
  ResultPath = "そんなパスはないよ!"
  Exit For
 End If
 ResultPath = ResultPath & "\" & FolderName
Next
MsgBox "これかな?" & vbCrLf & ResultPath
419: 07/17(木)20:37 ID:zBpNwhAR(2/4)調 AAS
>>417
正規表現の様な事ができたのでしょうか?
421: 07/17(木)21:05 ID:zBpNwhAR(4/4)調 AAS
>>417
本当に複数の階層で部分一致できますでしょうか?
426: 07/18(金)08:22 ID:avd6O0J8(1)調 AAS
>>417 はちょっと手抜きしてたので、ちゃんと作り直した。
Sub 使い方()
 Set Result = New Collection
 DirMulti "C:\Documents", "A???\B*\Excel\2025????\File*.xlsx", Result, vbNormal
 (以下略)
End Sub

Sub DirMulti(HomePath, SearchPath, ResultCollection, Optional Attributes As VbFileAttribute = vbNormal)
 Pos = InStr(SearchPath & "\", "\")
 Search = Left(SearchPath, Pos - 1)
 NextSearch = Mid(SearchPath, Pos + 1)

 Set SearchResult = New Collection
 Res = Dir(HomePath & "\" & Search, vbDirectory Or Attributes)
 Do While Res <> ""
  If Res <> "." And Res <> ".." Then SearchResult.Add Res
  Res = Dir()
 Loop

 If NextSearch = "" Then
  For Each Res In SearchResult
   ResultCollection.Add HomePath & "\" & Res
  Next
 Else
  For Each Res In SearchResult
   If (GetAttr(HomePath & "\" & Res) And vbDirectory) <> 0 Then
    DirMulti HomePath & "\" & Res, NextSearch, ResultCollection, Attributes
   End If
  Next
 End If
End Sub
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.023s