VBAでエクセルファイルを開く
2023/06/18
-
code ``` Private Function openFile(filePath As String, Optional myReadOnly = False)
Dim myFilename As String, myFile As Workbook
' If file path is filename, change it to a full path. If InStr(filePath, "\") = 0 Then filePath = thisFile.Path & "\" & filePath myFilename = Dir(filePath)
If myFilename = "" Then MsgBox filePath & " is not found." Exit Function End If
' If file which has same filename is open, return the file. For Each myFile In Workbooks If myFile.Name = myFilename Then Set openFile = myFile myFile.Activate Exit Function End If Next
Set openFile = Workbooks.Open(filePath, ReadOnly:=myReadOnly)
End Function ```