0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBAと共に去りぬ : コード集「基本編+基本編」をかけ合わせただけのレシピ

Last updated at Posted at 2019-11-23

特定フォルダのファイルを開く

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?