特定フォルダのファイルを開く
Sub openFile()
Dim buf As String, wb As Workbook
Dim cnt As Long
Const targetPath As String = "特定のフォルダ"
Dim excelFileName As String
excelFileName = InputBox("Please enter a part of filename. Folder is" & targetPath)
対象のフォルダ内のファイル全てを検索 一番最初のファイルがbufへ入る
buf = Dir(targetPath & "*")
Do While buf <> ""
If InStr(1, buf, excelFileName, vbTextCompare) > 0 Then
Exit Do
End If
buf = Dir() 'Dir()は自動的に次の値を吐き出す
Loop
'ファイルの存在チェック
If buf = "" Then
MsgBox buf & vbCrLf & "は存在しません", vbExclamation
Exit Sub
End If
'同名ブックのチェック
For Each wb In Workbooks
If wb.Name = buf Then
MsgBox buf & vbCrLf & "はすでに開いています", vbExclamation
Exit Sub
End If
Next wb
'Excelを開く場合、ユーザーフォームを閉じておかないと開けない
Unload UserForm1
'ファイルを開く
CreateObject("Shell.Application").ShellExecute targetPath & buf
End Sub
Kさんへ VBAのススメ SES業界で働いてる人は特におすすめ
https://www.youtube.com/watch?v=_3Yh85wlvQs